我有這樣的代碼:Javascript繼承由兩個subclases
var first = function() {
this.loc = '1';
this.test = function() {
console.log('first ' + this.loc);
}
}
var second = function() {
var me = this;
this.loc = '2';
this.test = function() {
console.log('second ' + this.loc);
Object.getPrototypeOf(me).test.call(me);
}
}
second.prototype = new first();
var third = function() {
var me = this;
this.loc = '3';
this.test = function() {
console.log('third ' + this.loc);
Object.getPrototypeOf(me).test.call(me);
}
}
third.prototype = new second();
var t = new third();
t.test();
它將輸出:
third 3
second 3
first 2
我怎樣才能使它輸出:
third 3
second 3
first 3
所以我想用最後一個繼承的類來覆蓋第一個類的loc值。
這是有趣的是,使用ES6語義我們看到預期的行爲:https://repl.it/EmmE/2 –