admin管理员组

文章数量:1277900

In my applications I am not sure if I am duplicating data when I pass it around to different objects. Just as an example have a look at the following code:

var DataStore = function(data) {
    this.data = data; // <-- typeof data === 'object'
}

var Controller = function() {
    var dataStore = new DataStore({foo: 'bar'});
    var plugin = new Plugin(dataStore.data);
}

var Plugin = function(data) {
   this.data = data;
}

var app = new Controller();

When I create Plugin it is passed the data property from dataStore. It is then assigned to a property inside Plugin. Keeping in mind that the data being passed around is an object my question is, is this creating two variables in memory or does the data property in Plugin reference the property in the DataStore object?

If it doesn't keep the reference after assignment, how can I pass the DataStore into Plugins and keep a reference to it locally? Or would I need to keep DataStore as a global variable in my application scope and reference it globally from the plugins?

In my applications I am not sure if I am duplicating data when I pass it around to different objects. Just as an example have a look at the following code:

var DataStore = function(data) {
    this.data = data; // <-- typeof data === 'object'
}

var Controller = function() {
    var dataStore = new DataStore({foo: 'bar'});
    var plugin = new Plugin(dataStore.data);
}

var Plugin = function(data) {
   this.data = data;
}

var app = new Controller();

When I create Plugin it is passed the data property from dataStore. It is then assigned to a property inside Plugin. Keeping in mind that the data being passed around is an object my question is, is this creating two variables in memory or does the data property in Plugin reference the property in the DataStore object?

If it doesn't keep the reference after assignment, how can I pass the DataStore into Plugins and keep a reference to it locally? Or would I need to keep DataStore as a global variable in my application scope and reference it globally from the plugins?

Share Improve this question asked Jul 17, 2012 at 5:20 Seain MalkinSeain Malkin 2,30319 silver badges20 bronze badges 2
  • AFAIK JavaScript objects are always references – Alvin Wong Commented Jul 17, 2012 at 5:25
  • 3 @AlvinWong Not quite. Objects in ECMAScript implementations like JavaScript can only be accessed through a reference, and object references are values (which can be assigned). Call-by-reference as known from other programming languages does not exist, it is always call-by-value. This is the same as, e.g., in Java. – PointedEars Commented Jul 17, 2012 at 5:32
Add a ment  | 

2 Answers 2

Reset to default 11

Both dataStore.data and plugin.data reference the same object - mutating the object in either of these "classes" (for lack of a better term) will result in the object being mutated for both of them (since they are both holding references to the same object).

Function arguments are always passed by reference in JS. When you pass an object as an argument, what gets passed is actually a pointer to the object's location in memory. If you attempt to overwrite the reference itself, nothing will happen to the original object, however if you change the value of any of the object's properties, the original object will be modified.

Ex:

function f1(_x) {
    _x = 5;
}

function f2(_y) {
    _y.name = 'Hello';
}

var x = 10;
f1(x);
console.log(x); // no change to x

var y = {name: 'Tom'};
f2(y);
console.log(y.name); // y.name is now Hello

本文标签: Javascript Passing objects by referenceStack Overflow