javascript“这个”再次指向Window对象

发布于 2024-08-31 04:34:28 字数 636 浏览 6 评论 0原文

我问了一个关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿童谣 2024-09-07 04:34:28

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

  • 函数调用
  • 方法调用
  • Apply/Call 调用
  • 构造调用

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

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

另一方面,当函数不是对象的属性时,则使用函数模式调用它。在本例中,this 参数绑定到全局对象,事实上,这就是 JavaScript 调用 callback() 函数的方式。

道格拉斯·克罗克福德在他的Good Parts book,将此描述为语言设计中的错误,并建议一些可能的解决方法。在您的情况下,一种简单的解决方法是使用 call()apply() 调用回调,如 Tim Down 在上一个问题中建议

callback.call(this);

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

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.

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).

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 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);

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

野味少女 2024-09-07 04:34:28

在 javascript 中,this 关键字被设置为函数的所有者。函数对象本身并不维护其所有权,而是从我们调用函数的方式推导出所有权。

例如:

var foo = function() {
    alert('hello');
};
var abc = {};
abc.bar = foo;

简单地调用函数就像

foo();

让解释器不知道该函数可能附加到哪个对象。它可能附加到多个对象,也可能是一个变量等。因此解释器将 this 设置为全局对象。

但是,当调用像

abc.bar();

解释器这样的函数时,知道该函数附加到 abc 对象,因此 this 设置为 abc。即使 barfoo 都引用同一个函数对象,调用模式的差异也会导致 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:

var foo = function() {
    alert('hello');
};
var abc = {};
abc.bar = foo;

Simply calling the function like

foo();

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

abc.bar();

the interpreter knows that function is attached to abc object, therefore this is set to abc. Even if both bar and foo refer to the same function object, the difference in the calling pattern causes this to behave differently.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文