We have a hypothetical function that concatenates strings:
function Concatenator(){
//Initializes the concatenator
this.delimiter = "-"
}
Concatenator.prototype = {
main_functionality: {
concatenate: function(array){
return array.join(this.delimiter)
}
}
}
It has to be initialized first (this is a complex process with async stuff). And then afterwards we can call methods in the prototype. However, methods in the prototype reference things created in the init stage, like this.delimiter.
//test it out
myArray = ['one', 'two', 'three'];
c = new Concatenator();
alert(c.main_functionality.concatenate(myArray));
See jsfiddle: https://jsfiddle.net/6j5rgjn3/
As you can see in the jsfiddle, it can call the methods OK but the delimiter is unavailable - it should show one-two-three
but instead shows `one,two,three'.
Question: How can c.main_functionality.concatenate reference this.delimiter?
I've tried:
1) calling this.delimiter instead this.prototype.delimiter like so but it doesn't work:
function Concatenator(){
//Initializes the concatenator
this.prototype = {}
this.prototype.delimiter = "-"
}
Concatenator.prototype.main_functionality = {
concatenate: function(array){
return array.join(this.delimiter)
}
}
2) Using .bind(this) after concatenate - no effect.
Any ideas?