javascript Javascript将对象传递给函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17360838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:01:36  来源:igfitidea点击:

Javascript passing object to function

javascriptclass

提问by Chris Morris

Quick question on Javascript to which I can't find a clear concise answer.

关于 Javascript 的快速问题,我找不到清晰简洁的答案。

I'm building an app that's way ahead of anything I've done before and involves multiple classes being instantiated. These objects are then passed into a processing class that checks user inputs, draws onto canvas and updates the objects that it has been passed.

我正在构建一个应用程序,它远远领先于我之前所做的任何事情,并且涉及多个被实例化的类。然后将这些对象传递到处理类中,该类检查用户输入、绘制到画布上并更新已传递的对象。

I am wondering, how does JavaScript handle passing objects to functions? Am I passing a copy of the object, or am I passing a reference to the object?

我想知道,JavaScript 如何处理将对象传递给函数?我是传递对象的副本,还是传递对象的引用?

So if my controller class alters one of the objects variables, is that changed everywhere or just in the object that that controller sees?

因此,如果我的控制器类更改了对象变量之一,它是随处更改还是仅在该控制器看到的对象中更改?

Sorry for such a simple, possibly easily testable question but I'm not even sure if I'm making a class correctly at this point thanks to errors piling up.

对于这么简单、可能很容易测试的问题,我很抱歉,但由于错误堆积,我什至不确定此时我是否正确地创建了一个类。

采纳答案by pai.not.pi

When passing in a primitive type variable like a string or a number, the value is passed in by value. This means that any changes to that variable while in the function are completely separate from anything that happens outside the function.

当传入像字符串或数字这样的原始类型变量时,值是通过 value 传入。这意味着在函数中对该变量的任何更改都与函数外发生的任何事情完全分开。

function myfunction(x)
{
      // x is equal to 4
      x = 5;
      // x is now equal to 5
}

var x = 4;
alert(x); // x is equal to 4
myfunction(x); 
alert(x); // x is still equal to 4

Passing in an object, however, passes it in by reference. In this case, any property of that object is accessible within the function

然而,传入一个对象是通过引用传入的。在这种情况下,该对象的任何属性都可以在函数内访问

function myobject()
{
    this.value = 5;
}
var o = new myobject();
alert(o.value); // o.value = 5
function objectchanger(fnc)
{
    fnc.value = 6;
}
objectchanger(o);
alert(o.value); // o.value is now equal to 6

回答by Lorenzo Marcon

As well described in https://stackoverflow.com/a/5314911/636348, in JavaScript it's always pass by value, but for objects the value of the variable is a reference.

正如https://stackoverflow.com/a/5314911/636348 中所述,在 JavaScript 中它总是按值传递,但对于对象,变量的值是一个引用。

So, it's not a pure pass by reference. Here's an example to understand this concept:

因此,它不是纯粹的引用传递。下面是一个理解这个概念的例子:

E.g.:

例如:

x = {member:"foo"}

If you change the object with another object inside a function, you won't get the new object outside the function scope because you just create another object. The original reference is still bound to the original object:

如果在函数内使用另一个对象更改对象,则不会在函数作用域之外获得新对象,因为您只是创建了另一个对象。原始引用仍然绑定到原始对象:

function changeObject(x) {
  x = {member:"bar"};
}

changeObject(x);
alert(x.member)

output: foo

输出:foo

instead, if you alter a memberinside a function, the object will be changed:

相反,如果您更改函数内的成员,则该对象将被更改:

function changeMember(x) {
  x.member = "bar";
}

changeMember(x);
alert(x.member)

output: bar

输出:酒吧

回答by Racheet

If you pass in a variable which is pointing to an object, it passes a reference to the object. If you pass in an object literal, then obviously no other class or function will be able to change that object.

如果传入一个指向对象的变量,它会传递对该对象的引用。如果您传入一个对象字面量,那么显然没有其他类或函数能够更改该对象。