Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

double double parentheses javascript

I found this code in a book:

function foo() {
  console.log( this.a );
}

var a = 2;
var o = { a: 3, foo: foo };
var p = { a: 4 };
o.foo(); // 3
(p.foo = o.foo)(); // 2

What does last line mean?

like image 577
user1272913 Avatar asked Dec 08 '25 10:12

user1272913


1 Answers

The last line is doing an assignment and then calling the function.

Assignment happens first

(p.foo = o.foo)

Then call the function

(p.foo = o.foo)();

In this second call to foo, it is being called outside of the scope of p or o, so it's essentially the same as calling:

foo();
like image 172
ohiodoug Avatar answered Dec 09 '25 22:12

ohiodoug