Home > Web Front-end > JS Tutorial > Associative prototype chain attribute characteristics in JavaScript_javascript skills

Associative prototype chain attribute characteristics in JavaScript_javascript skills

WBOY
Release: 2016-05-16 15:15:45
Original
1126 people have browsed it

Data attributes:

The data attribute contains the location of a data value from which the value can be read and written.

4 described behavioral characteristics:

writable indicates whether the value of the attribute can be modified. Default is true

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

configuralbe indicates whether the attribute can be deleted and redefined, and whether its configuration can be modified.

value contains the data value of this attribute. When reading attribute values, read from this location.

When writing attribute values, save the new values ​​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是obj对象的自有属性
alert("z" in obj) //=>true z是obj原型上继承来看属性
//hasOwnProperty 必须是对象上的自有的属性才返回true
alert(obj.hasOwnProperty("x")) //true x是obj对象上的自有属性
alert(obj.hasOwnProperty("z")) //false z是obj原型上继承来的属性,不是他的私有属性
alert(Foo.prototype.hasOwnProperty("z")) //=>true z是原型上自有的属性,所以返回true
alert(Object.prototype.hasOwnProperty("toString"))//=>toString 是顶级对象上的自有属性,所以返回true
//prpertyisEnumeralbe 的意思是必是对象上的自有属性而且要以是枚举的,但是对象的可枚举属性Enumeralbe是true,才能返回true
alert(obj.propertyIsEnumerable("x")) //true x是obj对象上可枚举的属性
alert(obj.propertyIsEnumerable("z")) //false z是obj原型上的属性,不是自有属性,则不可以枚举
alert(Foo.prototype.propertyIsEnumerable("z")) //true x是原型上的自有属性,所以可以枚举
</script>
Copy after login

How to enumerate properties? What is the difference between enumerated properties and prototypes?

The demo code is as follows:

<script>
function Foo(){}
Foo.prototype.age = 
var obj = new Foo()
obj.name = "ziksang"
obj.addr = "上海"
obj.telephone = 
for(var p in obj){ //使用FOR IN 可以枚举出自身的属性和原型上的属性
console.log(p)
}
console.log(Object.keys(obj)) //使用Object.keys(obj)只可以枚举Obj对象上自身的属性
console.log(Object.getOwnPropertyNames(obj)) //Object.getOwnPropertyNames(obj)是列出Ojb对象上自身的属性名,与枚举不相关,但是又类似枚举,大家要注意区分
</script>
Copy after login

The above content is the knowledge about the associated prototype chain attribute characteristics in JavaScript introduced by the editor. I hope it will be helpful to you.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template