Using Javascript, what's the difference between an expression in the context of assigning variables and out of context?

Spoiler

I am trying to solve the problem # 8 of this Javascript injection game.

In one of Erling Ellingsen 's comments, I found this interesting snippet.

(_ = []. concat) () [0]

What is the difference between the above snippet and the one

([]. concat) () [0]

What will change when assigning a variable to a [].concatvariable? It is clear that he is simply trying to access the global window object, but how do these two evaluate differently?

+4
source share
1 answer

, , , ECMAScript , (.. window). , ECMAScript 5, undefined, Array.prototype.concat .

ES3, this undefined null (, func()), .

ES5 , undefined null, .


, , - .

GetValue() - , - "", ().

eval():

var a = 0;
function test()
{   var a = 1, b;
    console.log(eval("a")); // 1
    console.log((b=eval)("a")); // 0
}
test();

, , :

var a = [].concat;
// called as variable, not property
a(); // therefore it global

// same as the following, as all these expressions call GetValue() interally
(0, [].concat)();
(random = [].concat)();
([].concat || 0)();

// but this doesn't work
[].concat(); // `this` is the new array, not global object
+2

All Articles