javascript" this"再次指向Window对象 [英] javascript "this" points to Window object again

查看:94
本文介绍了javascript" this"再次指向Window对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Javascript指向Window对象上提出了一个问题这指向Window对象。

I asked a question on Javascript this points to Window object regarding "this" points to Window object.

这里是源代码

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写道但是然后使用callback()调用该函数,这意味着它不会被调用作为一种方法,因此这是全球对象。

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?

当test2中的console.log(this)指向Window时,它如何指向Window.action ???

How does console.log(this) in test2 points to Window when it is inside archive.action???

推荐答案

在JavaScript中,您可以使用4种不同的调用模式调用函数:

In JavaScript you can invoke functions using 4 different invocation patterns:


  • 函数调用

  • 方法调用

  • 申请/呼叫调用

  • 施工调用

  • Function invocation
  • Method invocation
  • Apply/Call invocation
  • Construction invocation

这些模式的主要区别在于这个参数的初始化。

The patterns mainly differ in how the this parameter is initialized.

当你使用 oArchive时.action.test2(),您将使用方法模式调用 test2()函数,在本例中为将绑定到操作对象。只要调用表达式包含细化(即点表达式或 [下标] ,JavaScript将使用方法模式)表达式)。

When you use oArchive.action.test2(), you would be invoking the test2() function with the method pattern, and in this case this would be bound to the action object. JavaScript will use the method pattern whenever the invocation expression contains a refinement (i.e. the . dot expression or the [subscript] expression).

另一方面,当函数不是对象的属性时,则使用函数模式调用它。在这种情况下,参数绑定到全局对象,实际上这就是JavaScript调用 callback()的方式 function。

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 your callback() function.

Douglas Crockford 在他的好的零件书,将此描述为语言设计中的错误,并提出了一些可能的解决方法。在你的情况下,一个简单的解决方法是使用 call() apply()来调用回调,如< a href =https://stackoverflow.com/questions/2719643/javascript-this-points-to-window-object/2719696#2719696> Tim Down在您之前的问题中建议:

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() or apply(), as Tim Down suggested in your previous question:

callback.call(this);

这是有效的,因为Apply / Call调用模式允许您选择的值这个,这就是你需要的。

This works because the Apply/Call invocation pattern lets you choose the value of this, which is what you require.

这篇关于javascript&quot; this&quot;再次指向Window对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆