double double parentheses javascript

亡梦爱人 提交于 2020-01-02 08:55:12

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/34486148/double-double-parentheses-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!