Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

原型和实例属性的相关方法 #15

Open
cy0707 opened this issue Oct 8, 2016 · 0 comments
Open

原型和实例属性的相关方法 #15

cy0707 opened this issue Oct 8, 2016 · 0 comments

Comments

@cy0707
Copy link
Owner

cy0707 commented Oct 8, 2016

isPrototypeOf()

这个 isPrototypeOf()方法是用来检查某个对象是否为某个对象的原型。如果是的话,那么返回true,如果不是的话,那么返回false。

Person.isPrototypeOf(person1);
//Person是否为person1的原型,如果是的话,返回true。不是的话,返回false;

object.getPrototypeOf()

取得原型对象。

var proto = {};
var obj = Object.create(proto);
Object.getPrototypeOf(obj) === proto; // true

hasPrototypeProperty和hasOwnProperty

1.hasPrototypeProperty这个方法是在,给定属性存在与对象原型中,才返回true,不然返回false。
2.hasOwnProperty这个方法是在,给定属性存在与对象实例中,才返回true,不然返回false。

for in

for in循环时,返回的是所有能够通过对象访问的,可枚举的属性。其中既包括存在与实例中的属性,也包括了存在与原型中的属性。这里屏蔽了原型中不可枚举属性的实例属性也会在for in中出现。因为所有开发人员定义的属性都是可枚举的。(注意在这里IE8及更早的浏览器中有一个bug,那就是屏蔽不可枚举的实例属性不会出现在for in 中)

object.keys()方法

该方法接受一个对象作为参数,返回一个包含所有可枚举属性的字符串数组。

object.getOwnPropertyNames()方法

该方法会得到所有实例属性,无论它是否可枚举。

Object.create() 方法

该方法接受两个参数:一个用作新对象原型的对象和(可选的)一个为新对象定义额外属性的对象。

var person = {
    friends: ["a", "b"]
};
var test1 = Object.create(person);
     test1.friends.push("c");
console.log(person.friends); //["a", "b", "c"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant