admin管理员组文章数量:1415436
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.
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. 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 .js/ and the member callback G3D._file_loaded
has a wrong this
pointer when called sometimes.
Why, and how do I fix it?
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.
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. 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./barebones.js/ and the member callback G3D._file_loaded
has a wrong this
pointer when called sometimes.
Why, and how do I fix it?
Share Improve this question asked Aug 23, 2012 at 12:05 WillWill 75.7k43 gold badges175 silver badges255 bronze badges 2- Does this article answer your question? – Markus Hedlund Commented Aug 23, 2012 at 12:09
-
1
The value of
this
is determined by the particular situation of each invocation of a function. It doesn't matter that a function was originally defined as the value of a property of some object - it doesn't matter at all. What matters is the way that the function is called, and that's it. – Pointy Commented Aug 23, 2012 at 12:14
3 Answers
Reset to default 8There are 4 ways to use a function in Javascript
what each of these does is change what the content of this
is :
- function calls: this = global object (window in browser)
- method calls: this = object it is called from.
- constructor calls: this = new object you are creating.
- 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.
The this
keyword refers to the calling context. 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
.
Furthermore, if a reference to window
is what you want, why not use window
instead of 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:
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. It simply harnesses the power given to you by closures :D
本文标签: Javascript this objectwindow in member functionStack Overflow
版权声明:本文标题:Javascript `this` object == `window` in member function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745235573a2649021.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论