aralejs / class

Class Utility
aralejs.org/class
102 stars 88 forks source link

如何理解判断this.constructor === SubClass? #17

Open Tonyhark opened 9 years ago

Tonyhark commented 9 years ago

下面这段代码及注释思考了很久还是不太明白,这里判断this.constructor === SubClass 具体的用途是什么。 什么情况下 this.constructor !== SubClass? this.constructor === SubClass 又意味着什么? 请大神赐教 // Only call initialize in self constructor. if (this.constructor === SubClass && this.initialize) { this.initialize.apply(this, arguments) }

seanpan commented 9 years ago

function Parent(){} function Child(){} Child.prototype = new Parent(); console.log(new Child().constructor); // function Parent(){}

这意味着prototype的不谨慎赋值将影响constructor的查找。