双倍括号javascript [英] double double parentheses javascript

查看:73
本文介绍了双倍括号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

最后一行是什么意思?

推荐答案

最后一行正在进行作业,然后调用该函数。

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

首先发生作业

(p.foo = o.foo)

然后调用函数

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

在第二次调用foo时,它被调用到p或o的范围之外,所以它与调用基本相同:

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

这篇关于双倍括号javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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