Content Highlights:
Each object has a prototype (prototype), class, and extensibility associated with it
I. Prototype properties
1. The prototype property of an object is used to inherit properties, and this property is so important that we often call the "0 prototype attribute" directly "O's prototype".
2. The properties of the prototype are set at the beginning of the instance object creation: objects created by the object's direct volume use Object.prototype as their prototypes. Objects created with new use the prototype property of the constructor as their prototype. Objects created by Object.create () use the first parameter (or NULL) as their prototype.
3. In ES5, the object is passed in as a parameter to object.getprototypeof () to query its prototype. In ES3, there is no equivalent function, but the expression O.constructor.prototype is often used to detect the prototype of an object.
Objects created with the new expression typically inherit a constructor property, which refers to the constructor that creates the object.
Note: Objects created by the object's direct amount or object.create () contain a property named constructor, which refers to the object () function. Therefore, Constructor.prototype is the real prototype of the object's direct volume, but it is often not the case with objects created by Object.create ().
4. To detect whether an object is a prototype of another object (or in a prototype chain), use the isPrototypeOf () method. For example:
var p = {X:1}; Defining a prototype Object
var o = object.create (p); Use this prototype to create an object
P.isprototypeof (o)//=>true:o inheritance P
Object.prototype.isPrototypeOf (O)//=>true:p inherited from Object.prototype
It is important to note that the functions implemented by the isprototypeof () function are very similar to the instanceof operators
Two. Class properties
1. The class attribute of the object (class attribute) is a string that represents the type information of the object. Both ES3 and ES5 do not provide a way to set this property, and there is only one indirect way to query it. The default ToString () Method (inherited from Object.prototype) returns a string of the following format:
[Object,class]
The 2.classof () function can return the class of any object passed to it:
function Classof (o) {
if (o = = = null) return "NULL";
if (o = = = undefined) return "undefined";
return Object.prototype.toString.call (o). Slice (8,-1);
}
Extracts the characters from the 8th to the penultimate position of a returned string.
3. Objects created with built-in constructors (such as array and date) contain "Class properties," which match the constructor name. The host object also contains a meaningful "class attribute", but this is related to the specific JS implementation.
The class properties of objects created through the object's direct amount and object.create are "object", as are the objects created by those custom constructors, which are also "object",
Therefore, for a custom class, there is no way to differentiate an object's class by its Class property:
Classof (NULL)//=> "NULL"
Classof (1)//=> "number"
Classof ("")//=> "String"
Classof (False)//=> "Boolean"
Classof ({})//=> "Object"
Classof ([])//=> "Array"
Classof (/./)//=> "Regexp"
Classof (new Date ())//=> "Date"
Classof (window)//=> "window" (This is the client host object)
function f () {}; = = Define a custom constructor
Classof (new F ())//=> "Object"
Three. Extensibility
1. The extensibility of the object is used to indicate whether a new property can be added to the object. All built-in objects and custom objects are explicitly extensible, and the extensibility of the host object is defined by the JS engine.
In ES5, all built-in objects and custom objects are extensible, unless they are converted to non-extensible, and the extensible nature of the host object is also defined by the JS engine that implements the ES5.
2.ES5 defines the functions that are used to query and set object extensibility. The object is determined to be extensible by passing it into object.esextensible ().
If you want to convert an object to non-extensible, you need to call Object.preventextensions () and pass in the object to be converted as a parameter.
Note: Once an object is converted to non-extensible, it can no longer be converted back to extensible.
It is also important to note that preventextensions () only affects the extensibility of the object itself. If you add a property to the prototype of a non-extensible object, the non-extensible object also inherits these new properties.
3. The purpose of the extensible attribute is to "lock" the object to avoid outside interference.
The extensibility of objects is often used in conjunction with the configurable and writable nature of properties, and some of the functions defined by ES5 make it easier to set multiple properties
Similar to 4.object.seal () and Object.preventextensions (), in addition to being able to set an object to be non-extensible, you can also set all of the object's own properties to be non-configurable.
That is, you cannot add a new property to this object, and its existing properties cannot be deleted or configured, but its existing writable properties can still be set.
For those who have been closed (sealed) up the object can not be solved. You can use object.issealed () to detect whether an object is closed.
Object.freeze () will lock the object more closely----freeze. In addition to setting an object to be non-extensible and setting its properties to be non-configurable, you can also set all of its own data properties to read-only (if the accessor property of an object has setter methods, accessor properties are unaffected and can still be called by assigning values to the property). Use Object.isfrozen () to detect whether an object is frozen.
Object.preventextensions (), Object.seal (), and Object.freeze () all return incoming objects, that is, they can be called by means of nested functions:
Creates a closed object, including a frozen prototype and a non-enumerable property
var o = object.seal (Object.create (Object.freeze ({x:1})), {y:{value:2,writable:true}});
JS authoritative Guide Learn to summarize the three properties of--6.8 objects