javascript闭包和对象引用 [英] javascript closures and object reference

查看:116
本文介绍了javascript闭包和对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点模糊的情况。主要是因为我以为我已经抓住闭包。所以基本上我想要的是重置为默认值一个集合。所以让我们说我有集合,它有对象参数数组的构造函数。

i am a bit obscure situation. mainly because i thought i already grasp closures. so basically what i want is to reset to default values a collection. so let say i have collection which has constructor with array of objects parameter.

 var c = new collection([{x},{y},{z}]);

然后收集定期获取更新。因为我不是保持数组的初始值,喜欢重置为初始值。

then collection periodically get updated.since i am not keeping somewhere initial values of array, after sometime i would like to reset to initial values.

现在我不是问如何实现这个,可能有多种方式我的问题关于闭包本身。请进一步阅读

这样我可能认为使用闭包捕获这个初始值,因此它可能如下所示。

so the way i might thought to trap this initial value using closures so it might look like this.

c.on('reset',(function(arr){

    return function(){

          c.internalarray = arr;

    }

 })(c.internalarray))

所以它不工作似乎是因为引用作为参数传递集合更新了被捕获的 arr 也得到更新其结束对于

so it is not working seems because the reference is passed as argument the collection updates the suppossedly trapped arr also get updated its end up always true for

arr === c.internalarray;

我可能认为传递数组的克隆,但是重点不是在某处创建复制数据与分配保持全局变量。

i might thought to pass the clone of array but what is the point is not that somewhere just creating a copy of data with assigning keeping global variable.

所以我的问题我做错了。我想以某种方式隐式的javascript引擎创建一个被捕获的变量/对象的副本。

so my question what i am doing wrong. i thought that somehow implicitly javascript engine creates a copy of trapped variable/object. that i dont have to keep track of them.

推荐答案

在javascript中,复合数据类型通过引用传递,因此, c.internalarray arr 都指的是相同的值。实际上,你是在正确的方式,但你必须制作一个数组的副本,然后将其传递给您立即调用的函数。例如:

In javascript, composite data types are passed by reference, so, c.internalarray and arr both refer to the same value. Actually, you were on the right way, but you have to make a copy of the array before passing it to your immediately invoked function. For example :

c.on('reset', function (arr) {
    return function () {
        c.internalarray = arr;
    }
}(c.internalarray.slice()));

也就是说,这种技术不会创建深层副本,这意味着数组中包含的复合类型仍然可以从外部立即调用的函数。在下面的代码中,我试图简化这种情况,以使事情更容易理解,希望:

That said, this technique will not create a deep copy, meaning that composite types contained in the array are still mutable from the outside of the immediately invoked function. In the code below, I've tried to simplify this situation in order to make things easier to understand, hopefully :

变量 a 是指包含两个元素的数组:

The variable a refers to an array which contains two elements :

var a = [
    1, // primitive value
    {} // composite value
];

让我们做一个 a 分配给 b

var b = a.slice();
// a -> [1, {}]
// b -> [1, {}]
// b === a -> false

a 删除第一个元素对 b 没有影响:

Removing the first element from a has no effect on b :

a.shift();
// a -> [{}]
// b -> [1, {}]

但是修改 code>还影响 b

a[0].k = 'value';
// a -> [{ k: "value" }]
// b -> [1, { k: "value" }]
// a[0] === b[1] -> true

这篇关于javascript闭包和对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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