js中的new做了什么?


function Person () {
    this.name = name;
    this.age = age;
    this.sex = sex
 
    this.sayName = function () {
        return this.name;
    };
}
 
var person = new Person("tom", 21, "famle");
 
console.log(person.name);

使用关键字new创建新实例对象经过了以下几步:

  1. 创建一个新对象
  2. 将新对象的_proto_指向构造函数的prototype对象
  3. 将构造函数的作用域赋值给新对象 (也就是this指向新对象)
  4. 执行构造函数中的代码(为这个新对象添加属性)
  5. 返回新的对象
var Obj = {};

Obj._proto_ =  Person.prototype();

Person.call(Obj);

猜你喜欢

转载自blog.csdn.net/weixin_41910848/article/details/81983740