Java Reference
In-Depth Information
// Add a color property and set it to undefined, and then, perform the check
colorPoint2D.color = undefined;
colorExists = "color" in colorPoint2D;
print("Property color exists: " + colorExists + ", color = " +
colorPoint2D.color);
Property x exists: true, x = 10
Property color exists: false, color = undefined
Property color exists: true, color = undefined
The following statement creates an object with three properties; two of them
contain data values and another a function. It shows you how to use the property that is a
function. You need to call the function passing the arguments, if any:
// A person object with fName, lName, and getFullName properties
var john = {fName: "John",
lName: "Jacobs",
getFullName: function () {
return this.fName + " " + this.lName;
}
};
var fullName = john.getFullName();
print("Full name is " + fullName);
Full name is John Jacobs
Notice the use of the keyword
this
inside the function. The keyword
this
refers to
the object on which the function is called.
■
if the data property of an object has a function as its value, such a function is
known as a method of the object. in other words, a method is a function defined as a
property of an object.
Tip
Search WWH ::
Custom Search