I am creating an attribute in a javascript object by replacing some strings in an existing object, as a side effect I want to make some additional changes to a third property, which I try to access with this.property however in the replace function this is referring to the window instead of my ‘master’ object. How can I pass in the encapsulating object, so that I can use this to access third property.
b = {
a: 'onetwothree',
count: 0,
rp: function () {
this.c = this.a.replace(/e/g, function (str, evalstr) {
this.count++; // <-- this is refering to window.
return 'e' + this.count
})
}
};
b.rp();
b.c = 'oneNaNtwothreNaNeNaN whereas I want it to be one1twothre2e3
1 Answer