Link prototype chain property features in JavaScript, javascript prototype
Data attributes:
The data attribute contains the location of a data value, where the value can be read and written.
Four descriptive behavior characteristics:
Writable indicates whether the attribute value can be modified. The default value is true.
Enumerable indicates whether the returned attributes of the for in loop can be enumerated.
Delealbe indicates whether the delete attribute can be used to delete the attribute, and whether the attribute can be modified.
Value contains the data value of this attribute. Read from this location when reading the property value.
When writing the property value, save the new value to this location. The default value of this feature is true.
<Script> function Foo () {} Foo. prototype. z = var obj = new Foo () obj. x = alert ("x" in obj) // => true x is the self-owned attribute of the obj object alert ("z" in obj) // => true z indicates the property inherited from the obj prototype. // The hasOwnProperty must be the property of the object before truealert (obj. hasOwnProperty ("x") // true x is the self-owned attribute alert (obj. hasOwnProperty ("z") // false z is the property inherited from the obj prototype, not its private property alert (Foo. prototype. hasOwnProperty ("z") // => true z is the property of the prototype. Therefore, truealert (Object. prototype. hasOwnProperty ("toString") // => toString is the property of top-level objects, therefore, true // prpertyisEnumeralbe indicates that it must have its own attributes on the object and be enumerated. However, if Enumeralbe is true, truealert (obj. propertyIsEnumerable ("x") // true x is the enumerated attribute alert (obj. propertyIsEnumerable ("z") // false z is an attribute on the obj prototype. If it is not a self-owned attribute, alert (Foo. prototype. propertyIsEnumerable ("z") // true x is the property of the prototype, so it can be enumerated. </script>
How does one enumerate attributes? What are the differences between enumeration attributes and prototype?
The Demo code is as follows:
<Script> function Foo () {} Foo. prototype. age = var obj = new Foo () obj. name = "ziksang" obj. addr = "Shanghai" obj. telephone = for (var p in obj) {// you can use for in to enumerate attributes and properties console on the prototype. log (p)} console. log (Object. keys (obj) // use Object. keys (obj) can only enumerate the property console of the Obj object. log (Object. getOwnPropertyNames (obj) // Object. getOwnPropertyNames (obj) is used to list the attribute names of Ojb objects. It is irrelevant to enumeration, but similar to enumeration. You must differentiate between them. </script>
The above content is a small series of related knowledge about the properties of the prototype chain in JavaScript, I hope to help you.
Articles you may be interested in:
- JavaScript checks instance attributes and prototype attributes.
- Details about js instance attributes and prototype attributes
- How to Use js to modify prototype attributes
- JavaScript adds attributes to the prototype of the object in two ways:
- Research on prototype attributes in JScript