Twlig / issuesBlog

MIT License
3 stars 0 forks source link

new到底做了什么? #49

Open Twlig opened 2 years ago

Twlig commented 2 years ago

要创建 Person 的新实例,必须使用 new 操作符。

var obj = new Person();

new的过程中经历以下4个步骤:

(1) 创建一个空对象

obj = new Object();

(2)设置原型链

obj.__proto__ = Person.prototype;

(3)让Person中的this指向obj,并执行Person函数体

Person.call(obj);

(4) 返回新对象