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
2 Answers
Reset to default 11Both 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
版权声明:本文标题:Javascript: Passing objects by reference - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741249354a2365500.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论