Java Reference
In-Depth Information
Deleting Properties of an Object
You can delete a
configurable
property of an object using the
delete
operator. Its syntax is:
delete property;
The following snippet of code creates an object with two properties
x
and
y
, iterator
over the properties, delete the property named
x
, and iterates over the properties again.
The second time, the property
x
and its value are not printed because it has been deleted:
// Create an object with two properties x and y
var point = {x:10, y:20};
for(var prop in point) {
printf("point[%s] = %d", prop, point[prop]);
}
// Delete property x from the point object
delete point.x;
print("After deleting x");
for(var prop in point) {
printf("point[%s] = %d", prop, point[prop]);
}
point[x] = 10
point[y] = 20
After deleting x
point[y] = 20
In strict mode, it is an error to delete to a property whose
configurable
attribute is
set to
false
. Deleting a nonconfigurable property in nonstrict mode has no effect.
Using a Constructor Function
A constructor function (or simply a constructor) is a function that is used with the
new
operator to create an object. It is customary to start a constructor with an uppercase letter.
The following code creates a function named
Person
that is intended to be used as a
constructor:
// Declare a constructor named Person
function Person(fName, lName) {
this.fName = fName;
this.lName = lName;
this.fullName = function () {
return this.fName + " " + this.lName;
}
}
Search WWH ::
Custom Search