Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indirect function call in JavaScript

There are things like

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

But then there's this

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

"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') 

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

like image 704
Art Avatar asked Mar 01 '11 23:03

Art


People also ask

What is an indirect function call?

If the linker encounters an indirect function call (by a pointer to function), it assumes that any of the functions addresses of which were taken anywhere in the program, can be called at that point.

What is indirect invocation in JavaScript?

Indirect Invocation Pattern You can execute a JavaScript function indirectly using function's method as well. There are three function methods are used to execute JavaScript function indirectly. They are as follows, Call() method. Apply() method.

What is Call () in JavaScript?

The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.

What is direct and indirect function calling?

The main difference between the direct and the indirect call, is that: the direct call uses an instruction call with a fixed address as argument. After the linker has done its job, this address will be included in the opcode. the indirect call uses an instruction call with a register as argument (here rax ).


2 Answers

You are just using The Comma Operator.

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' 

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

For example:

var foo = 'global.foo';  var obj = {   foo: 'obj.foo',   method: function () {     return this.foo;   } };  obj.method();      // "obj.foo" (1, obj.method)(); // "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").

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"); })(); })(); 

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.

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')(); })(); 

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.

like image 70
Christian C. Salvadó Avatar answered Oct 11 '22 13:10

Christian C. Salvadó


It's the comma operator, which evaluates both of its operands and returns the value of the second one.

So, something like (null, alert) evaluates to the alert function, which you can immediately call using parentheses.

It's described in section 11.14 of ECMA-262 (PDF).

like image 28
Wayne Avatar answered Oct 11 '22 12:10

Wayne