[英]Variable store function as primitive type or reference type?
var f1 = function(){}
var f2 = f1;
It pointed to the same function as f1 or it store another copy from f1? 它指向的功能与f1相同,还是存储了f1的另一个副本?
The value of f2
is, like f1
, a reference to the same function. 像
f1
一样, f2
的值是对同一函数的引用。
You can use the fact that functions are objects to prove it : 您可以使用函数是对象这一事实来证明这一点:
var f1 = function(){}
var f2 = f1;
f1.a = 3;
console.log(f2.a); // logs 3
But as you can't change a function core, there is little practical use in knowing this, apart the fact you don't use much memory if you duplicate a function reference. 但是由于您无法更改函数核心,因此知道这一点几乎没有实际用途,此外,如果您复制函数引用,则不会使用太多内存。
It is really easy to prove using ===
使用
===
确实很容易证明
var f1 = function(){}
var f2 = f1;
console.log(f2===f1)
>>true
So f2
and f1
are pointing to the exact same function. 因此,
f2
和f1
指向完全相同的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.