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

Q21. Object.create(null) 和 {} 的区别 #24

Open
lstoryc opened this issue Jun 28, 2020 · 2 comments
Open

Q21. Object.create(null) 和 {} 的区别 #24

lstoryc opened this issue Jun 28, 2020 · 2 comments

Comments

@lstoryc
Copy link
Owner

lstoryc commented Jun 28, 2020

Object.create(null): 没有继承任何原型方法;

console.log(Object.create({}).toString);   // function toString() { [native code] }

console.log(Object.create(null).toString); // undefined
@lstoryc
Copy link
Owner Author

lstoryc commented Jun 28, 2020

Vue源码中经常会看到

var _event =Object.create(null);

好处就是防止自定义的属性/方法与原型链发生冲突

@lstoryc
Copy link
Owner Author

lstoryc commented Jun 28, 2020

另一个使用create(null)的理由是,在我们使用for..in循环的时候会遍历对象原型链上的属性,使用create(null)就不必再对属性进行检查了,当然,我们也可以直接使用Object.keys[]。

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