子で同じコンテキスト(つまり、このような)で親関数を呼び出したいと思います。OOP Javascript親関数を呼び出す方法
これについていくつかの調査を行いましたが、良い結果が得られていません。私が試したものは、未定義のエラーのプロパティを読み取ることができなくなってしまった。以下は私のコードです(簡略化し、短縮するコードをたくさん削除しています)。
テスト機能を呼び出す場合はテストしてください。私はコードにコメントをしました。
/* Buoy Class */
function Buoy() {
this.buoyID = 'buoy-' + getHoverID();
this.init = function() {
this.cacheDom();
this.bindEvents();
};
this.cacheDom = function() {
this.$buoy = $buoyContainer.find("#" + this.buoyID);
this.$buoyHeader = this.$buoy.find(".header");
this.$closeBuoy = this.$buoyHeader.find(".close-button");
};
this.bindEvents = function() {
this.$closeBuoy.on("click", this.closeBuoy.bind(this));
this.$buoyHeader.on("click", this.toggleBuoyMinimize.bind(this));
this.$buoy.on("click", this.focusBuoy.bind(this));
$(document).on("mousedown", this.unfocusBuoy.bind(this));
};
this.test = function(){
//THIS IS WHAT I WANT TO CALL
}
this.init();
};
CHILD
function LiveChat(userID) {
var _this = this;
Buoy.call(this);
this.init = function() {
this.cacheDom();
this.bindEvents();
};
this.cacheDom = function() {
this.$userInput = this.$buoy.find(".user-chat-input");
this.$conversationArea = this.$buoy.find(".conversation-area");
this.$loading = this.$buoy.find(".loading");
};
this.bindEvents = function() {
this.$userInput.on("input", this.setConversationAreaY.bind(this));
this.$userInput.on("keydown", this.sendMessage.bind(this));
this.$conversationArea.on("scroll", this.loadOlderMessages.bind(this));
this.$buoy.on("click", this.focusInput.bind(this));
}
//I WANT TO CALL TEST IN PARENT
//KEEPING THE CORRECT CONTEXT WITH .CALL(THIS)
this.init();
}
PARENTあなたはライブチャットがBouyを拡張したいFUNCTION
function extend(Child, Parent) {
var Tmp = function() {
};
Tmp.prototype = Parent.prototype;
Child.prototype = new Tmp();
Child.prototype.constructor = Child;
}
extend(LiveChat, Buoy);
extend(AddDream, Buoy);
をあなたは 'test'を呼び出す方法を知っている' init'を呼び出すことではなくする方法を知っているどのように来ますか? – Bergi
あなたの 'extend'関数は古くなっています。その 'Tmp'の代わりに' Child.prototype = Object.create(Parent.prototype); 'を使うべきです。 – Bergi
@Bergi私は自分のコードをCurtismorteが以下に述べたものに変更しました。私はまだ '未定義のプロパティ呼び出しを読むことができません'というエラーを受けています。 Buoy.test.callを呼び出す(this); – user7965134