JavaScript如何确定何时给函数调用“this”?背景? [英] How does JavaScript determine when to give a function call a "this" context?

查看:54
本文介绍了JavaScript如何确定何时给函数调用“this”?背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于显而易见的原因,在JavaScript中,以下两个调用是不同的:

For obvious reasons, in JavaScript, the following two calls are different:

foo.bar();

var bar = foo.bar;
bar();

即在第一次通话中, foo 对象。第二,它是对全球范围的参考。但是,以下示例不太直观:

Namely, in the first call, this is the foo object. In the second, it's a reference to the global scope. However, the following example is a little less intuitive:

(foo.bar)();

我希望它的运行方式与第二个例子相同,但实际操作方式与首先。也就是说,这个引用 foo ,而不是全局范围。

I would expect it to operate the same way as the second example, but it actually operates the same as the first. That is, this references foo, not the global scope.

什么是JavaScript的规则决定何时使函数调用方法调用以及何时简单地调用函数而没有特定的这个

What are JavaScript's rules for deciding when to make a function call a "method call" and when to simply call the function without a particular this?

Felix Kling 在评论中指出,我想知道为什么第三个例子不使用窗口上下文理论上它应该只是检索函数并调用没有额外的背景。他的例子澄清了我的问题:

As Felix Kling points out in a comment, I'm wondering why the third example doesn't use the window context when theoretically it should simply retrieve the function and call it without the additional context. His example clarifies my question a little:

(true && foo.bar)(); // 'this' refers to the global scope


推荐答案

那是一个棘手的问题,归结为ECMAScript标准的内部工作原理。 分组运营商的定义是:

That's a tricky one and boils down to the inner workings of the ECMAScript standard. The definition of the grouping operator is:


生产 PrimaryExpression :(表达式)的计算方法如下:


  1. 返回评估 Expression 的结果。这可以是参考类型。

  1. Return the result of evaluating Expression. This may be of type Reference.


添加注释:


此算法不会将 GetValue 应用于评估 Expression的结果。这样做的主要动机是, delete typeof 等运算符可以应用于带括号的表达式。

This algorithm does not apply GetValue to the result of evaluating Expression. The principal motivation for this is so that operators such as delete and typeof may be applied to parenthesised expressions.

所以这是关键:结果可以是参考。 Reference是一种内部数据类型,由基值和引用名称组成。

So this is the key: The result can be of type Reference. A Reference is an internal data type which consists of a base value and a referenced name.

例如,评估成员表达式 foo.bar ,导致带有基值 foo 的引用(对象)和引用的名称bar(只是标识符的字符串表示)。

For example evaluating the member expression foo.bar, results in a Reference with base value foo (the object) and the referenced name "bar" (simply a string representation of the identifier).

GetValue(ref) 实际访问对象属性并返回属性值的内部函数(这个例子中的函数对象)。大多数运营商在其操作数上调用 GetValue 确实解析这些引用,但不解析分组运算符。

GetValue(ref) is the internal function which actually accesses the property of the object and returns the value of the property (the function object in this examples). Most operators call GetValue on their operands do resolve these References, but not the grouping operator.

查看评估CallExpressions 的方式,也可以了解如何和参考资料有效。例如,一步是:

Looking at how CallExpressions are evaluated might also give an idea of how this and References work. For example, one step is:


thisValue 成为调用 ImplicitThisValue的结果 GetBase(ref)的具体方法

所以,如果你有一个参考值并尝试要调用它,这个的值将被设置为上述示例中的Reference( foo )的基值)。

So, if you have a Reference value and try to call it, the value of this will be set to the base value of the Reference (foo in the above example).

关于我的例子(true&& foo.bar)(); && operator 在其两个操作数上调用 GetValue(),因此分组运算符的结果不是Reference。

Regarding my example (true && foo.bar)();: The && operator calls GetValue() on both of its operands, so the result of the grouping operator is not a Reference.

这篇关于JavaScript如何确定何时给函数调用“this”?背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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