_javascript techniques for attribute characteristics of associative prototype chains in JavaScript

Source: Internet
Author: User
Tags hasownproperty

Data properties:

The Data property contains the position of a data value where the value can be read and written.

4 Description of behavioral characteristics:

Writable indicates whether the property's value can be modified. The default is True

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

Configuralbe indicate whether you can come over the delete attribute never redefined a property and can modify its configuration.

Value contains the data value of this property. Read the property value from this location.

When the property value is written, the new value is saved in this location. The default value for this attribute is true.

<script>
function foo () {}
foo.prototype.z = 
var obj = new Foo ()
obj.x = 
alert ("x" in obj)//=& Gt;true x is the own property
alert ("Z" in obj) of the Obj object//=>true z is inherited from the obj archetype to see the property
//hasownproperty must be its own property on the object to return True
Alert (Obj.hasownproperty ("x"))//true x is the own property
alert (Obj.hasownproperty ("Z") on the obj object)//false Z is an inherited property on the obj prototype. Not his private attribute
alert (Foo.prototype.hasOwnProperty ("z"))//=>true Z is a property on the prototype, so return true
alert ( Object.prototype.hasOwnProperty ("toString"))//=>tostring is the owning property on the top-level object, so returns True
//prpertyisenumeralbe It means that it must be a property on the object and is to be enumerated, but the object's enumerable property Enumeralbe is true to return True
alert (obj.propertyisenumerable ("x"))//true X is an enumerable attribute
alert (obj.propertyisenumerable ("Z")) on an obj object//false z is an attribute on the OBJ prototype, not its own property, you cannot enumerate
alert ( Foo.prototype.propertyIsEnumerable ("z"))//true X is its own property on the prototype, so you can enumerate the
</script>

How do you enumerate properties, and what are the differences between enumerated properties for prototypes?

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 to enumerate its own properties and the properties on the prototype
Console.log (p)
}
Console.log (Object.keys (obj))///Use Object.keys (obj) can only enumerate the properties on the Obj object itself
console.log object.getownpropertynames ( obj))//object.getownpropertynames (obj) is the name of the property that lists the OJB object itself, not related to enumerations, but like enumerations, you should be careful to differentiate
</script>

The above content is small to introduce the JavaScript associated prototype chain attribute characteristics of knowledge, hope to help everyone.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.