使用私有成员复制JavaScript对象 [英] Copy javascript object with private member

查看:74
本文介绍了使用私有成员复制JavaScript对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我四处张望,却找不到任何帮助。为什么在地球上我不能与私有成员一起克隆javascript对象而不使它们发生量子纠缠?

I've looked all around and found nothing to help me. Why on earth can't I clone a javascript object with private members without making them quantum entangled?

只看这段代码...这是一个带有getter的普通私有财产和二传手。
不知何故,如果我在一个实例上调用公共设置器,克隆的实例也会被更改。为什么?可以解决吗?

Just look at this code... It's a plain private property with getter and setter. Somehow if I call the public setter on one instance, the cloned one gets changed too. Why? Can it be worked around?

obj = function(){
    var changed = 0;
    this.getChanged = function(){
        return changed;
    }
    this.setChanged = function(){
        changed = 1;
    }
    this.setUnchanged = function(){
        changed = 0;
    }
};

myObj = new obj();
copiedObj = $.extend(true, {}, myObj); // Or any other deep copy function you'd have around

myObj.setChanged();
myObj.getChanged(); // returns 1
copiedObj.getChanged(); // returns 1!
copiedObj.setUnchanged();
copiedObj.getChanged(); // returns 0
myObj.getChanged(); // returns 0

感谢任何想法。

编辑:到目前为止,没有新内容。我知道javascript确实没有像Java或C ++这样的OO,但是嘿,我们在谈论编程语言,总有一种出路。有时候很丑,但是有一个。

Edit: So far, nothing new. I know javascript doesn't really have OO like Java or C++, but hey we're talking about programming languages, there is always one way out. Sometimes it's ugly but there is one.

我明白了。
解决方案A:使其变为 this.changed ,而不是 var更改

解决方案B:制作自己的克隆函数以重建整个结构新对象

I get it. Solution A: just make it this.changed instead of var changed
Solution B: make my own clone function that rebuilds the whole object anew

我只是希望有一些解决方案C可以将javascript欺骗成标准的面向对象模式。

I was just hoping there would be some Solution C that would trick javascript into standard Object Oriented patterns.

有人,我真的迷上了A还是B?

Somebody, am I really stuck with A or B?

推荐答案

问题是已更改不是私有变量-JavaScript没有私有变量。这是分配给 obj 变量的函数的局部变量。创建分配给 getChanged / setChanged / setUnchanged 属性的函数时,您正在创建关闭,已对变量已更改进行了关闭。

The problem is that changed isn't a private variable-- JavaScript doesn't have private variables. It's a local variable to the function you assign to the obj variable. When you create the functions that get assigned to the getChanged / setChanged / setUnchanged properties, you're creating closures that are closed over the variable changed.

克隆 myObj 时,您只是在为这些函数创建其他别名。因此,无论您是通过 myObj 还是 copiedObj 访问它们,您仍在调用相同的函数,并且由于它们是闭包,因此您访问的是完全相同的<在这两种情况下,strong> changed 变量都是如此。由于您无法复制函数,因此最好不要尝试将 changed 设为私​​有,而只是在第二行进行 this.changed = 0;

When you clone myObj, you're just creating additional aliases to those functions. So, you're still calling the same functions regardless of whether you access them through myObj or copiedObj, and because they're closures, you're accessing exactly the same changed variable in both cases. Since you can't copy functions, you're better off not trying to make changed private and just doing this.changed = 0; on the second line.

这篇关于使用私有成员复制JavaScript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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