我通过替换现有对象中的一些字符串来在 javascript 对象中创建一个属性,作为副作用,我想对第三个属性进行一些额外的更改,我尝试使用 this.property 访问它但是在替换函数中这是指的是窗口而不是我的“主”对象。我怎样才能传入封装对象,以便我可以this
用来访问第三个属性。
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
而我希望它是one1twothre2e3