Home >Web Front-end >JS Tutorial >Introduction to objects in JavaScript_javascript skills
In JavaScript, except number, string, boolean, null and undefined, all other values are objects. Objects can be declared directly through literals or created through the new operator. Unlike the Java language, properties in JavaScript objects can be dynamically added or deleted; at the same time, properties in objects can also be empty strings:
//empty string is allowed as object property
var o2 = {"":88, "p":99};
console.log(o2);//Object { =88, p=99}
//for constructor function, "new" operation returns an object.
function Computer(x, y) {
this.x = x;
this.y = y;
}
var c = new Computer(126, 163);
console.log(c);//Computer {x=126, y=163}
var c2 = new Computer(126);//missing parameter value will be "undefined"
console.log(c2);//Computer {x=126, y=undefined}
c.z = 66;
console.log(c);//Computer {x=126, y=163, z=66}
delete c.y;
console.log(c);//Computer {x=126, z=66}
If the function used when using the new operator to create a new object is not a class constructor, but just an ordinary function, then JavaScript will return an empty object after executing the function:
Object property
Object in JavaScript has the following 3 properties:
1.prototype. Reference, pointing to the prototype object of Object. Properties in prototype objects can be inherited by Object.
2.class. A string representing the class name of Object.
3.extensible. A boolean value indicating whether dynamic addition of properties is allowed in the Object. This property is only valid in ECMAScript 5.
Property attribute
Property in Object also has 3 attributes:
1.writable. Whether the property is writable.
2.enumerable. Whether the property will be enumerated when using the for/in statement.
3. configurable. Whether the properties of this property can be modified and whether the property can be deleted.