March 10, 2001 - Checking for a Certain Property | WebReference

March 10, 2001 - Checking for a Certain Property

Yehuda Shiran March 10, 2001
Checking for a Certain Property
Tips: March 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

Use the hasOwnProperty() method to find out if an object has a certain property you are interested in. The hasOwnProperty() is a method of the Object object, and thus all objects inherit it (all objects are subclasses of the Object object). The following code checks if the object Ken of the Employee subclass has a manager. Try it, it should give true:

function Employee() {
  this.dept = "HR";
  this.manager = "John Johnson";
}
function printProp() {
  var Ken = new Employee();
  alert(Ken.hasOwnProperty("manager"));
}