简体   繁体   English

修改在Javascript中通过引用传递的变量

[英]Modifying variables that are passed by reference in Javascript

I was watching a JavaScript talk, and the tutor said that if we pass a property of an object in a function it will actually change the real value, because we will be passing the variable by reference. 我正在观看JavaScript演讲,辅导老师说,如果我们在函数中传递对象的属性,它将实际上更改实际值,因为我们将通过引用传递变量。 Here is the slide: 这是幻灯片: 在此处输入图片说明

but when I tried to practice the concept, that wasn't the case. 但是当我尝试实践这一概念时,事实并非如此。 Here is my code: 这是我的代码:

var obj = {val: 5};
function changeVal(x) {
 x = x+5;
 return x;
}
console.log(obj.val) // 5
console.log(changeVal(obj.val)) // 10
console.log(obj.val) // 5

I was expecting obj.val to change to 10. Please tell me what's wrong here, and correct me if I am wrong. 我原本希望obj.val更改为10。 请告诉我这里出了什么问题,如果我错了,请纠正我。 Thanks 谢谢

You are passing not the object , but the primitive type . 您传递的不是对象 ,而是原始类型 So when you pass the val of the obj , it is a number and is a primitive type.It copies the val and passes the copy to the object. 因此,当您传递objval时,它是一个数字且是原始类型,它将复制val并将副本传递给该对象。

If you pass like this, it will work 如果您这样通过,它将起作用

var obj = {val: 5};

function changeVal( param ) {
 param.val = param.val  + 5;
 return param.val ;
}
console.log(obj.val) // 5
console.log(changeVal(obj)) // 10
console.log(obj.val) // 10

You are not actually passing an object, just passing the value of property(val). 您实际上并没有传递对象,只是传递了property(val)的值。 If you will pass obj in changeVal(), then it will actually change the value of the property of passed object. 如果要在changeVal()中传递obj ,那么它将实际上更改所传递对象的属性的值。

For that you need to do like: 为此,您需要这样做:

var obj = {val: 5};
function changeVal(x) 
{
    x = x+5;
    return x;
}
console.log(obj.val); // 5
changeVal(obj); // Need to pass object instead of value of the property's value
console.log(obj.val);  // 10

Primitive types (string, integer, boolean, etc...) are immutable, which means if you change one of the values inside a function, the callee (scope which calls your function) will not see the change. 基本类型(字符串,整数,布尔值等)是不可变的,这意味着如果您更改函数内部的值之一,则被调用方(调用您的函数的作用域)将看不到更改。

function doSomething(a) {
    a = a + 1;
}
var value = 2;
console.log(value); // result: 2
doSomething(value);
console.log(value); // result: 2

Pass-by-reference only works for objects. 引用传递仅适用于对象。 Like this: 像这样:

function doSomething(obj) {
    obj.attribute = obj.attribute + 1;
}
var myObject = {attribute: 2};
console.log(myObject.attribute); // result: 2
doSomething(myObject);
console.log(myObject.attribute); // result: 3

More reading about Javascript types: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures 有关Javascript类型的更多信息: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Data_structures

Say for instance you have an Iphone . 假设您有一部Iphone。 Now lets say a manufacturing company calls you and asks to borrow your Iphone for a reference just so they can design an Iphone that is similar and sell it to customers . 现在,假设一家制造公司打电话给您,并要求借用您的Iphone以作参考,以便他们可以设计相似的Iphone并将其出售给客户。 Your original Iphone still exists and is never gone , but every now and then the factory needs to use it for a reference , think of your function as the factory that just make a copy of obj . 您最初的Iphone仍然存在并且从未消失,但是工厂不时需要使用它作为参考,将您的功能obj仅复制obj的工厂。

//Original data

var obj = {val: 5};

Once your function returns something , it technically becomes a value 一旦函数返回值,从技术上讲,它就变成了一个值

Example : 范例:

return 3; is a value of 3

so 所以

function changeVal(x) {
 x = x+5;
 return x;
}

is a new value of x which in this case would be x + 5; 是x的新值,在这种情况下为x + 5;

x is a copy of whatever you pass into the function . x是传递给函数的任何内容的副本

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM