最简单的方法通常是将的值存储在
this局部变量中:
myObject.prototype = { ajax: function (url) { // (url argument missing ?) var instance = this; // <-- store reference to the `this` value this.foo = 1; var req = new XMLHttpRequest(); req.open('GET', url, true); req.onreadystatechange = function (aEvt) { if (req.readyState == 4) { if (req.status == 200) { alert(instance.foo); // <-- use the reference } } }; }};我也怀疑您的
myObject标识符确实是构造函数(您正在分配
prototype属性)。
如果是这种情况,请不要忘记添加正确的
constructor属性(因为您正在替换整个属性
prototype),这只是对构造函数的引用。
也许是这个问题的主题,但建议阅读:
- 建设者被认为有点混乱



