newPromise / js-

0 stars 0 forks source link

使用 Object.assign 实现原型链继承的时候的方法合并 #20

Open newPromise opened 6 years ago

newPromise commented 6 years ago
function person ( { name, home } ) {
}

Object.assign( person.prototype, { 
  showConstructor () {
    console.log( this.constructor ); // 指向 person
  }
 })

使用 下面这种方法,情况不会向我们想的那样:

person.prototype = {
  showConstructor () {
    console.log( this.constructor ); //  Object
  }
}

使用上面的这种方法,person.prototype 成为了一个对象,当然,对象的构造函数就是指向对象了