new的过程

24 阅读1分钟
  1. 创建一个新的空对象
  2. 将空对象的proto 指向了 Person.prototype
  3. 将构造函数内部的this指向新对象
  4. 执行构造函数给this(实例对象)添加属性或方法
  5. 默认return this
function Person(name,age){
  this.name = name 
  this.age = age 
}

Person.prototype.say = function(){
  console.log('说话')
}

var zs = new Person('张三',20)