In JavaScript, except for number, string, boolean, null and undefined, all other values are objects. Objects can be declared directly through literals or newly created through the new operator. Unlike Java language, properties in JavaScript objects can be added or deleted dynamically; at the same time, properties in objects can also be empty strings:
The code copy is as follows:
//properties in object can be added/deleted dynamically
var o = {x:1, y:2};
console.log(o);//Object {x=1, y=2}
delete oy;
oz = 3;
console.log(o);//Object {x=1, z=3}
//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}
cz = 66;
console.log(c);//Computer {x=126, y=163, z=66}
delete cy;
console.log(c);//Computer {x=126, z=66}
If the function that works is not a class constructor, but is just an ordinary function when using the new operator to create a new object, then JavaScript will return an empty object after executing the function:
The code copy is as follows:
//for pure function, "new" operation returns an empty object.
function compute(x){
console.log("execute function compute");
return x*2;
}
var a = new compute();
console.log(a);//compute {}
Object property
Object in JavaScript has the following 3 properties:
1.prototype. Reference, pointing to the prototype object of the Object. The property in the prototype object can be inherited by the Object.
2.class. A string, representing the class name of the Object.
3.extensible. boolean value, indicating whether dynamic addition of properties is allowed in the Object. This property is only valid in ECMAScript 5.
Property properties
Property in Object also has 3 properties:
1.writable. Is this property writable?
2.enumerable. When using the for/in statement, will the property be enumerated?
3.configurable. Whether the property's properties can be modified and whether the property can be deleted.