对象之间的指针行为 [英] Pointer behavior between objects

查看:103
本文介绍了对象之间的指针行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解的东西以及我观察到越来越多了。

I would like to understand well something i observe more and more.

在一些circonstances,来自同一型号不同的情况下,改变它们的属性相同的方式(如果我有2个的usermodel A和B,如果我改变A,B将被以同样的方式影响)。

In some circonstances, different instances from a same model change their attributes the same way (if i have 2 UserModel A and B, if i change A, B will be affected the same way).

我观察到一些实际情况:

I observed some practical cases:

当我发送一个模型的实例到一个视图构造函数,它们有联系,如果我改变在我看来,模型,外面的人会受到影响相同的方式。我想有时候我们只是给一个指针,而不是一个实例复制到视图。

When i send an instance of a model to a view constructor, they are linked, if i change the model in my view, the outside one will be affected the same way. I guess sometime we just send a pointer and not a copy of the instance to the view.

更具体一些code;

More specific with some code;

A = new UserModel();
B = new UserModel();

var Data = A.get('info'); //where info = {some: "thing"};
Data.some = 'other';
B.set('info', Data); //A.get('info') == B.get('info')

由于我得到了对象信息不仅单独的属性(我测试它,还有就是价值观这种方式之间没有repercution)。

Because i got the object info and not only the attributes separately (i tested it and there is no repercution between the values this way).

所以我的问题是,我们总是使用指针与JavaScript的对象呢?它是具体的骨干?我想了解什么是这种行为背后。

So my question is, are we always using pointers with objects in javascript ? Is it specific to backbone ? I would like to understand what is behind this behavior.

感谢。

推荐答案

对象和数组传递或指定为在JavaScript中,引用不拷贝。如果你想要一个对象或数组的副本,你必须明确地进行复制。

Objects and Arrays are passed or assigned as references in javascript, not copies. If you want a copy of an object or an array, you have to explicity make a copy.

赋值或传递时,简单的类型,如数字,布尔被复制。

Simpler types such as numbers, boolean are copied when assigned or passed.

字符串有点特殊情况。它们被作为参考过去了,但因为字符串是不可变的(不能改变),你不能真正用于任何字符串引用,因为任何试图修改字符串创建一个新字符串。

Strings are a bit of special case. They are passed as references, but since strings are immutable (can't be changed), you can't really use a string reference for anything because any attempt to modify the string creates a new string.

一对夫妇的例子:

// arrays assigned by reference
var a = [1,2,3];
var b = a;
a[0] = 0;
alert(b[0]);  // alerts 0 because b and a are the same array

// objects assigned by reference
var c = {greeting: "hello"};
var d = c;
c.greeting = "bye";
alert(d.greeting);   // alerts "bye" because c and d are the same object

// numbers assigned as copies
var e = 3.414;
var f = e;
e = 999;
alert(f);    // alerts 3.414 because f is its own copy of the original number

// make a copy of an array
var g = [1,2,3];
var h = g.slice(0);    // h is now a copy
h[0] = 9;
alert(g);              // shows [1,2,3]
alert(h);              // shows [9,2,3]

同样是从一个函数传递参数给一个函数或返回值真。除非建立一个明确的副本,数组和对象传递或引用返回。

The same is true for passing arguments to a function or returning values from a function. Unless an explicit copy is created, arrays and objects are passed or returned by reference.

数组的浅表副本可以用 .slice进行()方法。

A shallow copy of an array can be made with the .slice() method.

var arr1 = [1,2,3];
var arr2 = arr1.slice(0);   // make independent copy of first array

对象的浅表副本可以通过从原始对象的每个属性复制到一个新的对象进行。

A shallow copy of an object can be made by copying each property from the original object to a new object.

深拷贝涉及测试每个项目的类型被复制,并在递归的对象,​​如果它是这样嵌套对象和阵列被复制过的物体或阵列。

Deep copies involve testing the type of each item being copied and recursing on the object if it is an object or array so nested objects and arrays are copied too.

这篇关于对象之间的指针行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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