Object properties are defined as a simple association between name and value. All properties have a name and value is one of the attributes linked with the property, which defines the access granted to the property. Properties refer to the collection of values which are associated with the JavaScript object. This collection may not follow any particular order. JavaScript provides the feature to add, delete and modify the properties. Properties are denoted by name:values pairs.
Properties are the values associated with a JavaScript object.
A JavaScript object is a collection of unordered properties.
Properties can usually be changed, added, and deleted, but some are read only.
The JavaScript for...in
statement loops through the properties of an object.
for (variable in object) {
// code to be executed
}
The block of code inside of the for...in
loop will be executed once for each property.
Looping through the properties of an object:
var person = {fname:"John", lname:"Doe", age:25};
for (x in person) {
txt += person[x];
}
The delete
keyword deletes a property from an object:
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
delete person.age; // or delete person["age"];
The delete
keyword deletes both the value of the property and the property itself.
After deletion, the property cannot be used before it is added back again.
The
delete
operator is designed to be used on object properties. It has no effect on
variables or functions.
The delete
operator should not be used on predefined JavaScript object
properties. It can crash your application.
All properties have a name. In addition they also have a value.
The value is one of the property's attributes.
Other attributes are: enumerable, configurable, and writable.
These attributes define how the property can be accessed (is it readable?, is it writable?)
In JavaScript, all attributes can be read, but only the value attribute can be changed (and only if the property is writable).
( ECMAScript 5 has methods for both getting and setting all property attributes)
JavaScript objects inherit the properties of their prototype.
The delete
keyword does not delete inherited properties, but if you delete a
prototype property, it will affect all objects
inherited from the prototype.