Open Sogrey opened 4 years ago
new关键字与构造函数一起使用以创建对象:
new
function Employee(name, position, yearHired) { this.name = name; this.position = position; this.yearHired = yearHired; }; const emp = new Employee("Marko Polo", "Software Developer", 2017);
new关键字做了4件事:
4
{}
this
**proto**
prototype
return
看下面事例:
function Person() { this.name = '前端小智' }
根据上面描述的,new Person()做了:
new Person()
var obj = {}
**proto__
this.__proto** = Person().prototype
return this
new操作符具体干了什么呢? #36
new
关键字与构造函数一起使用以创建对象:new
关键字做了4
件事:{}
this
值**proto**
指向构造函数的prototype
return
语句,则返回this
看下面事例:
function Person() { this.name = '前端小智' }
根据上面描述的,
new Person()
做了:var obj = {}
this
值:this = obj**proto__
指向构造函数的prototype
:this.__proto** = Person().prototype
this
:return this
new操作符具体干了什么呢? #36