Associated stereotype chain attribute properties in JavaScript

  • 2020-12-19 20:55:02
  • OfStack

Data attributes:

The data property contains the location of a data value where values can be read and written.

4 described behavioral characteristics:

writable indicates whether the value of the property can be modified. The default is true

Enumerable indicates whether the for in loop return property can be enumerated.

configuralbe says can You come delete delete property never redefine the property, can you change its configuration.

value contains the data value for this attribute. When you read property values, you read from this location.

When you write the property value, save the new value in this location. The default value for this feature is true.


<script>
function Foo(){}
Foo.prototype.z = 
var obj = new Foo()
obj.x = 
alert("x" in obj) //=>true x is obj Object has its own properties 
alert("z" in obj) //=>true z is obj Inheritance on the prototype to see the properties 
//hasOwnProperty  Must be a property on the object to return true
alert(obj.hasOwnProperty("x")) //true x is obj Property on an object 
alert(obj.hasOwnProperty("z")) //false z is obj An inherited property on a prototype is not its private property 
alert(Foo.prototype.hasOwnProperty("z")) //=>true z Is a property on the stereotype, so return true
alert(Object.prototype.hasOwnProperty("toString"))//=>toString  Is a property on the top-level object, so returns true
//prpertyisEnumeralbe  Must be a property on the object and must be enumerable, but the object's enumerable property Enumeralbe is true, To return to true
alert(obj.propertyIsEnumerable("x")) //true x is obj Enumerable properties on an object 
alert(obj.propertyIsEnumerable("z")) //false z is obj An attribute on a prototype that is not its own cannot be enumerated 
alert(Foo.prototype.propertyIsEnumerable("z")) //true x Is a property on the prototype, so you can enumerate 
</script>

How do you enumerate attributes, and how do enumerations differ for stereotypes?

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){ // use FOR IN  You can enumerate your own properties and the properties on your prototype 
console.log(p)
}
console.log(Object.keys(obj)) // use Object.keys(obj) You can only enumerate Obj Property on the object itself 
console.log(Object.getOwnPropertyNames(obj)) //Object.getOwnPropertyNames(obj) Is to list Ojb The name of the property on the object itself, not related to enumeration, but similar to enumeration, you should be careful to distinguish 
</script>

The above content is the site to introduce you to the associated prototype chain attributes in JavaScript related knowledge, I hope to help you.


Related articles: