内部实现:
function ClassA(sColor) {this.color = sColorthis.sayColor = function () { console.log(this.color)}}function ClassB(sName) {this.name = sNamethis.sayName = function () { console.log(this.name)}}function ClassC(sColor, sName) {ClassA.call(this,sColor)ClassB.call(this,sName)}var objA = new ClassA("blue")var objC = new ClassC("red", "John")objA.sayColor()objC.sayColor()objC.sayName()
外部实现:
function ClassA(sColor) {this.color = sColorthis.sayColor = function () { console.log(this.color)}}function ClassB(sName) {this.name = sNamethis.sayName = function () { console.log(this.name)}}function ClassC(sColor, sName) {}var objA = new ClassA("blue")var objC = new ClassC()ClassA.call(objC,'red')ClassB.call(objC,'John')objA.sayColor()objC.sayColor()objC.sayName()
输出:
blueredJohn