在下面的代碼中面對繼承問題我是OOPS概念的新手,請幫助我。Javascript繼承不起作用
var testA = function() {
this._data = undefined
};
testA.prototype.init = function(data) {
this._data = data;
};
testA.prototype.get = function(data) {
if (this._data) {
return data
}
};
var testB = function() {}
testB.prototype = Object.create(testA);
var instance = new testB();
testB.prototype.init = function(data) {
testA.prototype.init.call(this, data);
};
testB.prototype.Somedata = function() {
testA.prototype.get();
}
instance.init("testdata");
instance.Somedata() // here i am getting this._data is undefined
當我打電話instance.init( 「TESTDATA」),現在它的設定值在父this._data。
當我打電話instance.Somedata()在這裏我得到未定義
可我知道可能是什麼原因?當我調用instance.Somedata()時,如何獲得this._data值。
您剛更改了代碼?問題是'testA.init'不存在,現在你已經編輯了錯誤。 – adeneo
是剛纔我編輯它 –
我可以知道我怎麼能得到this._data值,當我打電話給instance.somedata() –