Sogrey / Web-QA

https://sogrey.github.io/Web-QA/
MIT License
6 stars 2 forks source link

new 关键字有什么作用? #293

Open Sogrey opened 4 years ago

Sogrey commented 4 years ago

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件事:

看下面事例:

function Person() { this.name = '前端小智' }

根据上面描述的,new Person()做了:


new操作符具体干了什么呢? #36