在我的一些Javascript对象中,我发现我的this指针是正确的 - 这些是new Func()- 类型的对象 - 在创建时,但在指定的方法中它可能是错误的.
function Confused() {
console.log("checking",this==window,"is always false");
this.method = function() {
console.log("checking",this==window,"is true for some funcs but not others");
};
};
Run Code Online (Sandbox Code Playgroud)
在一些调用(new Confused()).method()- 它似乎失去了它的this指针.这种情况发生的时间似乎取决于功能,而不是随机的; 它在代码中的一些东西,我是如何创建导致这种情况的类.
一个例子是在http://williame.github.com/barebones.js/在线,并且有时调用成员回调时指针G3D._file_loaded错误this.
为什么,我该如何解决?
javascript ×1