变量/对象是否按值传递,为什么我不能在javascript中使用变量来更改对象的属性? [英] Are variables/objects passed by value and why can't I change object's property with variable in javascript?

查看:68
本文介绍了变量/对象是否按值传递,为什么我不能在javascript中使用变量来更改对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个对象:

  var obj = {
len:4,
bred:5
}

现在假设我将这个对象赋值给一个变量 x 作为 var x = obj; 。据我了解,它创建了一个 obj 的副本,并将该副本分配给 x - 这是通过值。现在,如果我更改 x 的属性,那么它也会更改obj对象的属性。例如

  x.len = 99 

然后 obj.len 和 x.len 变成 99 。另一方面考虑这种情况:

  var r = 2,s = 3,t = 4; 
s = r;
s = 88;

现在 r 按值传递给 s s 一份 r 赋予<$ ç$ C>取值。因此,将 s 更改为 88 不会更改 r 变量。在控制台输入 r 仍然会给出 2



问题1 :如果变量(包含的对象)在JavaScript中按值传递,那么为什么更改 x.len 更改原始 obj.len ,也是吗?



另一个问题是,我无法在分配给变量时更改对象的属性。考虑这种情况:

  var obj2 = {
len:4,
bred:5
}
var x2;
x2 = obj.len;

现在在控制台中输入 x2 code> 4 。但是,如果我尝试更改 x2 的值,例如 x2 = 77; ,那么不会更改 obj2.len



问题2:为什么我无法使用变量更改对象的属性?

解决方案

所有东西都是按值传递的,但是当您创建一个对象时,您将获得对该对象的引用。

  //创建一个对象。将该对象的引用赋值给`obj` 
var obj = {
len:4,
bred:5
};
//将`obj`的值复制到`x`。现在`x`也是该对象的引用。
var x = obj;

x obj 现在将修改同一个对象,因为它们通过同一个引用的副本。






  var obj2 = {
len:4,
bred:5
}
var x2;
x2 = obj.len;

len 是一个数字,不是对象,所以这个值不是一个参考。所以,当你复制它时,你会得到一个数字的副本(而不是该对象的引用的副本)。


Suppose I have an object as:

var obj = {
        len: 4,
        bred: 5
    }

Now suppose I assign this object to a variable x as var x = obj;. As far as I understand it, it creates a copy of obj and assign that copy to x — that is pass by value. Now If I change a property of x then it changes that property of the obj object too. E.g.

x.len = 99

Then both obj.len and x.len become 99. On the other hand consider this scenario:

var r = 2, s = 3, t = 4; 
s = r;
s = 88;

Now r is passed by value to s that s a copy of r was given to s. So changing s to 88 doesn't change the original value of r variable. Typing r in the console still gives 2.

Question 1: If variables (objects included) are passed by value in JavaScript then why does changing x.len change the original obj.len, too?

Another problem is that I cannot change an object's property when assigning to a variable. Consider this scenario:

var obj2 = {
        len: 4,
        bred: 5
    }
var x2;
x2 = obj.len;

Now typing x2 in console simply returns 4. But if I try to change the value of x2 such as x2 = 77; then that doesn't change obj2.len.

Question2: Why can't I change object's property with a variable?

解决方案

Everything is passed by value, but when you create an object you get an reference to that object.

// Create an object. Assign a reference to that object to `obj`
var obj = {
    len: 4,
    bred: 5
};
// Copy the value of `obj` to `x`. Now `x` is also a reference to that object.
var x = obj;

Any changes to properties of x or obj will now modify the same object because they go through copies of the same reference.


var obj2 = {
    len: 4,
    bred: 5
}
var x2;
x2 = obj.len;

len is a number, not an object, so the value isn't a reference. So when you copy it you get a copy of the number (instead of a copy of the reference to the object).

这篇关于变量/对象是否按值传递,为什么我不能在javascript中使用变量来更改对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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