简体   繁体   English

成员函数中的Javascript`this`object ==`window`

[英]Javascript `this` object == `window` in member function

In some of my Javascript objects I find that my this pointer is correct - these are new Func() -type objects - when created, but in the assigned methods it can be wrong. 在我的一些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");
   };
};

On some calls to (new Confused()).method() - it seems to have lost it's this pointer. 在某些调用(new Confused()).method() -它似乎已经失去了它的this指针。 The times this happen seem dependent on the function, rather than random; 这种情况发生的时间似乎取决于功能,而不是随机的; its something in the code around how I'm creating classes that is causing this. 它在代码中的一些东西,我是如何创建导致这种情况的类。

An example is online at http://williame.github.com/barebones.js/ and the member callback G3D._file_loaded has a wrong this pointer when called sometimes. 一个例子是在网上http://williame.github.com/barebones.js/和成员回调G3D._file_loaded有错this指针有时也被称为时。

Why, and how do I fix it? 为什么,我该如何解决?

There are 4 ways to use a function in Javascript what each of these does is change what the content of this is : 有4种方法在JavaScript中使用的函数每个这些确实是什么改变了什么内容this就是:

  • function calls: this = global object (window in browser) 函数调用:this =全局对象(浏览器中的窗口)
  • method calls: this = object it is called from. 方法调用:this =从中调用它的对象。
  • constructor calls: this = new object you are creating. 构造函数调用:this =您正在创建的新对象。
  • call/apply calls: this = object you passed. 呼叫/应用呼叫:此=您传递的对象。

In your case this == window when you call the function directly ( Confused() ) but if you call using new ( new Confused() ) then it will be the new object you are creating. 在你的情况下, this == window直接调用函数( Confused() ),但如果你使用new( new Confused() )调用,那么它将是你正在创建的新对象。

The this keyword refers to the calling context. this关键字引用调用上下文。 It is never wrong but it may not be what you would have expected it to be. 它永远不会错,但它可能不是你所期望的那样。

You can manually set context when calling a function, if you call it with .call or .apply . 调用函数时,您可以手动设置背景下,如果你把它.call.apply

Furthermore, if a reference to window is what you want, why not use window instead of this ? 此外,如果您想要window的引用,为什么不使用window而不是this It's a way more reliable means of accessing the window object. 这是一种更可靠的访问窗口对象的方法。

In addition to David Hedlunds explanation, you can "work around" that problem like this: 除了David Hedlunds的解释,你可以“解决”这个问题:

function Confused() {
   var that = this;
   console.log("checking",that==window,"is always false");
   this.method = function() {
       console.log("checking",that==window,"is always false");
   };
};

The problem is that the whoever actually invokes your function has control over context of your function. 问题在于,实际调用函数的人可以控制函数的上下文。 If you do not control the function invocation (that is, if you can't modify the code), than you are stuck with the solution I gave (at least I know of no other way). 如果你不控制函数调用(也就是说,如果你不能修改代码),那么你会坚持使用我给出的解决方案(至少我不知道其他方式)。

While the solution seems a little "hackish", it really isn't if you think about it. 虽然解决方案看起来有点“hackish”,但如果你想一想,它真的不是。 It simply harnesses the power given to you by closures :D 它只是利用闭包给你的力量:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM