I have developed a draft of a library, and coming from a OOP Java background, have a problem setting/accessing the correct "this"-keyword.
Each instance is linked to a canvas, which it draws to using various settings, instantiated like so (commands return this, so chainable):
var ffc = new FatFontCreator()
.setup({
canvasID : "#mapCanvas",
nestingDepth: 5
})
.drawNumber(args);
The functionality of the object is set in the prototype property:
FatFontCreator.prototype.settings = {
dataSet : {
highestMagnitude : 5,
someFunction : function() {
console.log(this);
}
}
etc...
The main problem I have is to ensure the value of this to be the relevant object, as I expect some functionality to be called as a callback.
In order to set the this I bind them within the constructor, which replaces the existing functions of the object:
this.settings.dataSet.someFunction = this.settings.dataSet.someFunction.bind(this);
Which is not working since it changes the actual prototype function, and as you can only bind a function once, it doesn't work for more than one instance.
EDIT: The constructor looks like this
function FatFontCreator() {
this.canvas = undefined;
this.canvasContext = undefined;
etc.
return this;
}
My question: is there an easy way to bind this without overwriting the prototype method? Alternatively, will I have to declare all of the functions which I want to bind to the current instance in the constructor?
Any help would be much appreciated!