通过对函数的引用传递对象,这是一种意外的行为

时间:2021-11-11 20:59:50

So i have this piece of code where i try to mutate an object by passing it as a reference to a function:

所以我有这段代码,我试图通过将一个对象传递给一个函数来改变它:

var c = { why: 'older' };
var d;
d = c;
//passing by reference
function changeGreeting(obj) {
    obj.why = 'newer' ; // mutate         
}
changeGreeting(d)
console.log(c);
console.log(d);

This mutates successfully and outputs as expected : 通过对函数的引用传递对象,这是一种意外的行为 No problems until so far..

这将成功地实现和预期的输出:到目前为止没有问题。

Then i have the evil twin code looks the same but does't behave:

然后我有邪恶的孪生代码看起来一样,但却没有行为:

var c = { why: 'older' };
var d;
d = c;
//passing by reference, i wish..
function changeGreeting(obj) {
    obj = { why: 'newer' }; // trying to mutate..        
}
changeGreeting(d)
console.log(c);
console.log(d);

I would expect this to work the same way but it doesn't(it fails to mutate). 通过对函数的引用传递对象,这是一种意外的行为 Looking for a good clear explanation why?

我希望它能以同样的方式工作,但它不会(它不会发生突变)。为什么要找一个清楚的解释?

3 个解决方案

#1


3  

This code obj = { why: 'newer' }; does not mutate, it just assigns to a local variable obj inside your function.

这个代码obj ={为什么:“更新”};它不会变异,它只是在你的函数中赋值给一个局部变量obj。

To mutate the object, you need to use property assignment.

要修改对象,需要使用属性赋值。

In other words, obj points to some object (contains a reference to that object), so you can mutate it. By reassigning the value to other object, you are replacing that reference to original object with reference to your new object.

换句话说,obj指向某个对象(包含对该对象的引用),因此您可以修改它。通过将值重新分配给其他对象,您将使用对新对象的引用替换对原始对象的引用。

#2


2  

Your function basically behaves as following

函数的基本行为如下

function changeGreeting(obj) {
    var obj; // creates the local variable
    obj = d; // assign the arguments (objects are passed by reference)
    obj = { why: 'newer' }; // Creates new reference for local object        
}

Because of the above behavior, the references for c and d are preserved and continue to point to the same memory location before the function call.

由于上述行为,c和d的引用被保留,并在函数调用之前继续指向相同的内存位置。

Edit

编辑

The first scenario behaves like the following

第一个场景如下所示

function changeGreeting(obj) {
    var obj; // creates the local variable
    obj = d; // assign the arguments (objects are passed by reference)
    obj.why = 'newer'; // Updates local variable obj which has same reference as "d" and "c" 
}

#3


1  

When you do obj = { why: 'newer' }; You are modifying the function scoped variable obj to a reference to a new object { why: 'newer' }.

当你做obj ={为什么:'更新'};您正在将函数作用域的变量obj修改为对新对象{为什么:'更新'}的引用。

#1


3  

This code obj = { why: 'newer' }; does not mutate, it just assigns to a local variable obj inside your function.

这个代码obj ={为什么:“更新”};它不会变异,它只是在你的函数中赋值给一个局部变量obj。

To mutate the object, you need to use property assignment.

要修改对象,需要使用属性赋值。

In other words, obj points to some object (contains a reference to that object), so you can mutate it. By reassigning the value to other object, you are replacing that reference to original object with reference to your new object.

换句话说,obj指向某个对象(包含对该对象的引用),因此您可以修改它。通过将值重新分配给其他对象,您将使用对新对象的引用替换对原始对象的引用。

#2


2  

Your function basically behaves as following

函数的基本行为如下

function changeGreeting(obj) {
    var obj; // creates the local variable
    obj = d; // assign the arguments (objects are passed by reference)
    obj = { why: 'newer' }; // Creates new reference for local object        
}

Because of the above behavior, the references for c and d are preserved and continue to point to the same memory location before the function call.

由于上述行为,c和d的引用被保留,并在函数调用之前继续指向相同的内存位置。

Edit

编辑

The first scenario behaves like the following

第一个场景如下所示

function changeGreeting(obj) {
    var obj; // creates the local variable
    obj = d; // assign the arguments (objects are passed by reference)
    obj.why = 'newer'; // Updates local variable obj which has same reference as "d" and "c" 
}

#3


1  

When you do obj = { why: 'newer' }; You are modifying the function scoped variable obj to a reference to a new object { why: 'newer' }.

当你做obj ={为什么:'更新'};您正在将函数作用域的变量obj修改为对新对象{为什么:'更新'}的引用。