javascript“这个”再次指向Window对象
我问了一个关于 Javascript this 指向 Window 对象 关于“this”点的问题到窗口对象。
这是蒂姆·唐(Tim Down)编写的源代码
var archive = function(){}
archive.prototype.action = {
test: function(callback){
callback();
},
test2: function(){
console.log(this);
}
}
var oArchive = new archive();
oArchive.action.test(oArchive.action.test2);
“但随后使用回调()调用该函数,这意味着它不是作为方法调用,因此这是全局对象”。
按实际名称调用函数与源代码所示的callback() 调用有什么区别?
test2 中的 console.log(this) 在 archive.action 中如何指向 Window?
I asked a question on Javascript this points to Window object regarding "this" points to Window object.
here is source code
var archive = function(){}
archive.prototype.action = {
test: function(callback){
callback();
},
test2: function(){
console.log(this);
}
}
var oArchive = new archive();
oArchive.action.test(oArchive.action.test2);
Tim Down wrote "but that function is then called using callback(), which means it is not called as a method and hence this is the global object".
What are differences between calling a function by its actual name and callback() as shown on the source code?
How does console.log(this) in test2 points to Window when it is inside archive.action???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 JavaScript 中,您可以使用 4 种不同的调用模式来调用函数:
这些模式的主要区别在于
this
参数的初始化方式。当您使用
oArchive.action.test2()
时,您将使用方法模式调用test2()
函数,在本例中为this
将绑定到action
对象。只要调用表达式包含细化(即.
点表达式或[subscript]
表达式),JavaScript 就会使用方法模式。另一方面,当函数不是对象的属性时,则使用函数模式调用它。在本例中,
this
参数绑定到全局对象,事实上,这就是 JavaScript 调用callback()
函数的方式。道格拉斯·克罗克福德在他的Good Parts book,将此描述为语言设计中的错误,并建议一些可能的解决方法。在您的情况下,一种简单的解决方法是使用
call()
或apply()
调用回调,如 Tim Down 在上一个问题中建议:这有效,因为 Apply/Call 调用模式允许您选择
this
,这就是您所需要的。In JavaScript you can invoke functions using 4 different invocation patterns:
The patterns mainly differ in how the
this
parameter is initialized.When you use
oArchive.action.test2()
, you would be invoking thetest2()
function with the method pattern, and in this casethis
would be bound to theaction
object. JavaScript will use the method pattern whenever the invocation expression contains a refinement (i.e. the.
dot expression or the[subscript]
expression).On the other hand, when a function is not the property of an object, then it is invoked using the function pattern. In this case, the
this
parameter is bound to the global object, and in fact this is how JavaScript is invoking yourcallback()
function.Douglas Crockford in his Good Parts book, describes this as a mistake in the design of the language, and suggests some possible workarounds. In you case, one easy workaround would be to invoke the callback using
call()
orapply()
, as Tim Down suggested in your previous question:This works because the Apply/Call invocation pattern lets you choose the value of
this
, which is what you require.在 javascript 中,
this
关键字被设置为函数的所有者。函数对象本身并不维护其所有权,而是从我们调用函数的方式推导出所有权。例如:
简单地调用函数就像
让解释器不知道该函数可能附加到哪个对象。它可能附加到多个对象,也可能是一个变量等。因此解释器将
this
设置为全局对象。但是,当调用像
解释器这样的函数时,知道该函数附加到
abc
对象,因此this
设置为abc
。即使bar
和foo
都引用同一个函数对象,调用模式的差异也会导致this
的行为不同。In javascript the
this
keyword is set to the owner of a function. Function objects do not maintain their ownership themselves, instead the ownership is deduced from the way we call a function.eg:
Simply calling the function like
gives the interpreter no clue about what object the function might be attached to. It may be attached to multiple objects, it may be a variable etc. So the interpreter sets
this
to the global object.But however, when calling a function like
the interpreter knows that function is attached to
abc
object, thereforethis
is set toabc
. Even if bothbar
andfoo
refer to the same function object, the difference in the calling pattern causesthis
to behave differently.