JavaScript中的间接函数调用 [英] Indirect function call in JavaScript

查看:82
本文介绍了JavaScript中的间接函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有类似的事情

f.call(...)
f.apply(...)

然后就是这个

(1, alert)('Zomg what is this????!!!11')

1在这种情况下似乎没有多大意义,以下工作正常:

"1" does not seem to mean much in this context, the following works just fine:

(null, alert)('Zomg what is this????!!!11')
(1, null, alert)('Zomg what is this????!!!11')
(undefined, alert)('Zomg what is this????!!!11')

你能指出一下描述语法的ECMAScript的一个特定部分吗?

Could you point to a specific part of ECMAScript which describes that syntax?

推荐答案

你刚才使用逗号运算符

此运算符仅评估其操作数从左到右,并从第二个返回值,例如:

This operator only evaluates its operands from left to right, and returns the value from the second one, for example:

(0, 1); // 1
('foo', 'bar'); // 'bar'

在调用函数的上下文中,操作数的评估只会得到一个值,而不是引用,这会导致调用函数内的 this 值指向全局对象(或者它将是 undefined 在新的ECMAScript 5 Strict Mode中。

In the context of calling a function, the evaluation of the operand will simply get a value, not a reference, this causes that the this value inside the invoked function point to the global object (or it will be undefined in the new ECMAScript 5 Strict Mode).

例如:

var foo = 'global.foo';

var obj = {
  foo: 'obj.foo',
  method: function () {
    return this.foo;
  }
};

obj.method();      // "obj.foo"
(1, obj.method)(); // "global.foo"

如你所见,第一个电话,这是一个直接电话,方法中的值将正确引用 obj (返回obj.foo),第二次调用,逗号运算符进行的评估将使这个指向全局对象的值(产生global.foo)。

As you can see, the first call, which is a direct call, the this value inside the method will properly refer to obj (returning "obj.foo"), the second call, the evaluation made by the comma operator will make the this value to point to the global object (yielding "global.foo").

该模式已经获得这些天非常流行,使间接调用 eval ,这在ES5严格模式下很有用,例如,获取对全局对象的引用(假设您处于非浏览器环境中,窗口不可用):

That pattern has been getting quite popular these days, to make indirect calls to eval, this can be useful under ES5 strict mode, to get a reference to the global object, for example, (imagine you are in a non-browser environment, window is not available):

(function () {
  "use strict";
  var global = (function () { return this || (1,eval)("this"); })();
})();

在上面的代码中,内部匿名函数将在严格模式代码单元中执行,这将导致将值设为 undefined

In the above code, the inner anonymous function will execute within a strict mode code unit, that will result on having the this value as undefined.

|| 运算符现在将采用第二个操作数, eval 调用,这是一个间接调用,它将评估关于全局词汇和变量环境的代码。

The || operator will now take the second operand, the eval call, which is an indirect call, and it will evaluate the code on the global lexical and variable environment.

但就个人而言,在这种情况下,在严格模式下我更喜欢使用函数获取全局对象的构造函数:

But personally, in this case, under strict mode I prefer using the Function constructor to get the global object:

(function () {
  "use strict";
  var global = Function('return this')();
})();

使用函数构造函数创建的函数只有当它们以Use Strict Directive开头时它们才是严格的,它们不像函数声明或函数表达式那样继承当前上下文的严格性。

Functions that are created with the Function constructor are strict only if they start with a Use Strict Directive, they don't "inherit" the strictness of the current context as Function Declarations or Function Expressions do.

这篇关于JavaScript中的间接函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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