I want to call the parent function with the same context (ie..this) in a child.
I'v done some research on this, and haven't come up with good results. The ones that I have tried have resulted in not being able to read property of undefined errors. Here is my code (I have simplified and remove lots of code to shorten) :
test if the function I want to call. I have made comments in the code.
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
/* 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();
};
EXTEND 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);