Hi,
I have this piece of code:
var myObj = function () {
this.complex = function (text) { /* long piece of code */ }
this.parse(text) {
return text.replace(/valid_pattern/gi, function ($1) { return this.complex($1); } );
}
}
Of course calling this.complex($1) won't do the trick, because I'm in the scope of the anonymous function. I can't re-scope the anonymous function using .call(this) statement either, because in th ta case I would lose the parameters passed to the function by String.replace.
So far I'm using the concrete instance of the object. This is my solution:
var instance = new myObj;
var myObj = function () {
this.complex = function (text) { /* long piece of code */ }
this.parse(text) {
return text.replace(/valid_pattern/gi, function ($1) { return instance.complex($1); } );
}
}
So far it's sufficient to my needs, but I'm wondering if there is any universal solution to this problem. The only idea that has worked for me so far is this:
function ($1) { return (new myObj).complex($1); }
... which suffers from serious performance issues. Any ideas would be greatly appreciated.
-- D.
P. S. Sorry about my English, it's not my first language.