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

js中__proto__和prototype的区别和关系? #99

Open
JesseZhao1990 opened this issue Apr 7, 2018 · 1 comment
Open

js中__proto__和prototype的区别和关系? #99

JesseZhao1990 opened this issue Apr 7, 2018 · 1 comment

Comments

@JesseZhao1990
Copy link
Owner

JesseZhao1990 commented Apr 7, 2018

https://www.zhihu.com/question/34183746

untitled diagram

@JesseZhao1990
Copy link
Owner Author

Object.setPrototypeOf 和Object.getPrototypeOf

1. Object.setPrototypeOf

Object.setPrototypeOf(obj,proto) 用来将obj.__proto__设置为proto。

该方法相当于修改一个对象的内置属性[[Prototype]],是一个非常慢的操作,因为现代浏览器或JavaScript引擎都对属性访问进行过优化。修改内置属性,是一件影响范围很大的事情,并不仅仅意味着赋值obj.proto = xxx,而是可能会涉及所有用过该属性的代码,以及可能访问该属性的对象。

考虑到性能,应该尽可能避免使用该方法,而是使用Object.create方法,根据给定原型创建一个新对象。

2. Object.getPrototypeOf

获取一个对象的内置属性[[Prototype]]

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