简体   繁体   English

'值是一个引用'对JavaScript对象意味着什么

[英]What does 'value is a reference' mean for JavaScript objects

I was trying to understand passing of variables and objects in JavaScript, and came across this page . 我试图理解JavaScript中变量和对象的传递,并且遇到了这个页面

It is explained that JavaScript objects are passed by value, but the value itself is a reference. 解释了JavaScript对象是按值传递的,但值本身是一个引用。 Though I understood what was happening in the examples given on that page, I am still confused so as to why. 虽然我理解该页面上给出的例子中发生了什么,但我仍然感到困惑。 Can anyone please explain what does "value itself is a reference" mean? 任何人都可以解释一下“价值本身是一个参考”是什么意思吗?

Some languages have a "pass by reference" concept for function arguments which means that when you call a function and pass in a variable by reference the function can modify the original variable to hold some other value - it has a reference to the original variable. 有些语言对函数参数有一个“按引用传递”的概念,这意味着当你调用一个函数并通过引用传入一个变量时函数可以修改原始变量来保存一些其他值 - 它有一个对原始变量的引用。

With "pass by value" when you call a function and pass in a variable the function only gets the value so can't change the original variable that was passed in. 当您调用函数并传入变量时,通过“按值传递”,函数只获取值,因此无法更改传入的原始变量。

JS only has "pass by value", however when you pass an object as a parameter the "value" is a reference to the original object such that the function can modify, create or delete properties of that object, but the function can't modify the original variable to refer to some other object or value. JS只有“按值传递”,但是当您将对象作为参数传递时,“value”是对原始对象的引用,以便该函数可以修改,创建或删除该对象的属性,但该函数不能修改原始变量以引用其他一些对象或值。

Example: 例:

function changeObj(someObj) {
    someObj.a = 1000;
    someObj.c = "test";

    someObj = { "x" : 5 };
    console.log(someObj);   // { "x" : 5 }
}

var o = { "a" : 1, "b" : 2 };
changeObj(o);
console.log(o);   // { "a" : 1000, "b" : 2, "c" : "test" }

The code I've shown creates a variable, o , that references an object with a and b properties. 我展示的代码创建了一个变量o ,它引用了一个带有ab属性的对象。 It then calls the function changeObj and passes in o . 然后它调用函数changeObj并传入o The function changes the value of the a property and creates a new c property - the function is modifying the same object that variable o refers to because it has a reference to that object. 该函数更改a属性的值并创建一个新的c属性 - 该函数正在修改变量o引用的同一对象,因为它具有对该对象的引用。 But then the function assigns someObj equal to a completely new object. 但是然后函数将someObj指定为一个全新的对象。 This does not affect o at all because the function only had a reference to the object o was pointing at, it didn't have access to the o variable itself. 不会影响o ,因为在所有的功能只需要对象的引用o在指指点点,它没有进入o变量本身。

Like Java, Python, and many other languages, in JavaScript, objects are not values . 与Java,Python和许多其他语言一样,在JavaScript中, 对象不是值

What that means is that, when you evaluate an expression, the value you get from that is either a primitive or a reference (a reference is a pointer to an object). 这意味着,当您计算表达式时,从中获得的值是基元或引用(引用是指向对象的指针)。 When you create an object, that expression evaluates to a reference. 创建对象时,该表达式将计算为引用。 When you access a field of an object or call a method on an object, the thing on the left is a reference. 当您访问对象的字段或在对象上调用方法时,左侧的东西是引用。 Basically, anything you do with objects must be done through a reference to the object. 基本上,您对对象所做的任何事情都必须通过对对象的引用来完成。 There is no syntax for dealing with objects directly. 没有直接处理对象的语法。

You cannot have a variable whose value "is" an object (unlike C++, where you can have both a variable whose value is an object, and a variable whose value is a pointer that points to an object); 你不能拥有一个值为“是”对象的变量(与C ++不同,你可以同时拥有一个值为对象的变量,以及一个值为指向对象的指针的变量); you can only have a variable whose value is a reference that points to an object. 您只能拥有一个变量,其值是指向对象的引用。

This is apparent from the fact that when you assign a variable, a new object is never created. 这一事实很明显,当您分配变量时,永远不会创建新对象。 When you assign a reference, the assigned variable has a copy of the value of the original reference, and thus points to the same object as the original reference. 分配引用时,分配的变量具有原始引用值的副本,因此指向与原始引用相同的对象。 There is nothing that you can put in a variable that causes assigning it to create a new object. 没有任何东西可以放在变量中,导致分配它来创建一个新对象。

So if someone says "pass an object to a function", I say, No, you cannot pass an object, because objects are not values. 因此,如果有人说“将对象传递给函数”,我会说,不,你不能传递一个对象,因为对象不是值。 You must be passing a reference to an object. 您必须传递对象的引用。 Like in assignment, when you pass a reference, the value of it is copied. 与赋值一样,当您传递引用时,将复制它的值。 JavaScript is always pass-by-value. JavaScript始终是按值传递的。

It is explained that JavaScript objects are passed by value, but the value itself is a reference. 解释了JavaScript对象是按值传递的,但值本身是一个引用。

That's rather confusing wording. 这是相当混乱的措辞。 What they mean is that when objects are passed to functions, the reference value which points to the object is passed instead of the actual object. 它们的意思是当对象传递给函数时,将传递指向对象的引用值而不是实际对象。

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

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