reference type vs. primitive type
-
Hi, I know that objects in javascript, like in many other languages, holds the reference to the object itself and primitive types like number bool holds the actual value. I have something like this:
function manager()
{var anObject = {prop1: 1, prop2: 2};
.
.
.return { getMyObject: function(){ return anObject; }
}
My question is why when I do something like this:
var m = new manager();
var o = m.getMyObject();
o = {a: 10, b: 20};The actual value in 'o' which is a reference type is not changed when I do this:
var o2 = m.getMyObject();
what is actually set in the variable 'o'? and how can I make it persist in the above
-
Hi, I know that objects in javascript, like in many other languages, holds the reference to the object itself and primitive types like number bool holds the actual value. I have something like this:
function manager()
{var anObject = {prop1: 1, prop2: 2};
.
.
.return { getMyObject: function(){ return anObject; }
}
My question is why when I do something like this:
var m = new manager();
var o = m.getMyObject();
o = {a: 10, b: 20};The actual value in 'o' which is a reference type is not changed when I do this:
var o2 = m.getMyObject();
what is actually set in the variable 'o'? and how can I make it persist in the above
Your code currently does the following:
- Store the object returned from
getMyObject
in a variable calledo
; - Store a new object in the
o
variable; - Store the object returned from
getMyObject
in a variable calledo2
;
It should be fairly obvious why
o
ando2
do not point to the same object - step 2 makeso
point to a new, different object. If you want your changes to the object to be persisted for future calls of thegetMyObject
function, you need to change the properties of the object returned from that function:var m = new manager();
var o = m.getMyObject();o.prop1 = 10;
o.prop2 = 20;var o2 = m.getMyObject();
// o2 contains { prop1: 10, prop2: 20 }
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
- Store the object returned from
-
Your code currently does the following:
- Store the object returned from
getMyObject
in a variable calledo
; - Store a new object in the
o
variable; - Store the object returned from
getMyObject
in a variable calledo2
;
It should be fairly obvious why
o
ando2
do not point to the same object - step 2 makeso
point to a new, different object. If you want your changes to the object to be persisted for future calls of thegetMyObject
function, you need to change the properties of the object returned from that function:var m = new manager();
var o = m.getMyObject();o.prop1 = 10;
o.prop2 = 20;var o2 = m.getMyObject();
// o2 contains { prop1: 10, prop2: 20 }
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks for your reply Richard, I understood the sequence but there's still something not clear for me. A variable which holds a reference, basically holds a pointer to an object. If I set variable 'a' which holds a reference to object1 to a new variable called 'b', both of them will have a pointer that should point to the same place i.e. object1. Maybe I'm confusing it with a language like c# but if I set a new object to variable 'a' called 'object2', I'm creating a new pointer to that new object. I expect the place in memory where the original pointer which pointed to 'object1' will change and point to object2 for both 'a' and 'b'. But I see that's not happening. 'a' points to the new object while 'b' still points to the older one.
- Store the object returned from
-
Thanks for your reply Richard, I understood the sequence but there's still something not clear for me. A variable which holds a reference, basically holds a pointer to an object. If I set variable 'a' which holds a reference to object1 to a new variable called 'b', both of them will have a pointer that should point to the same place i.e. object1. Maybe I'm confusing it with a language like c# but if I set a new object to variable 'a' called 'object2', I'm creating a new pointer to that new object. I expect the place in memory where the original pointer which pointed to 'object1' will change and point to object2 for both 'a' and 'b'. But I see that's not happening. 'a' points to the new object while 'b' still points to the older one.
No. If variables
a
andb
point toobject1
, and you change variablea
to point toobject2
, variableb
will not be affected. If you're more comfortable with C#, consider the following contrived example:string a = string.Empty;
string b = string.Empty;a = "Hello";
What value do you think will be in
b
after you run this code? If the language followed your idea, since both variables start out pointing to the same string, when you changea
to point to the string"Hello"
, it would automatically changeb
as well. That would make it virtually impossible to write a program, since all variables and fields of the same type would automatically be pointing to the same instance of that type. Think of it like postal addresses: persona
lives ataddress1
, and personb
also lives ataddress1
. If you change the street-name foraddress1
, that change affects both people. If persona
moves toaddress2
, that doesn't necessarily mean that personb
moves as well.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
No. If variables
a
andb
point toobject1
, and you change variablea
to point toobject2
, variableb
will not be affected. If you're more comfortable with C#, consider the following contrived example:string a = string.Empty;
string b = string.Empty;a = "Hello";
What value do you think will be in
b
after you run this code? If the language followed your idea, since both variables start out pointing to the same string, when you changea
to point to the string"Hello"
, it would automatically changeb
as well. That would make it virtually impossible to write a program, since all variables and fields of the same type would automatically be pointing to the same instance of that type. Think of it like postal addresses: persona
lives ataddress1
, and personb
also lives ataddress1
. If you change the street-name foraddress1
, that change affects both people. If persona
moves toaddress2
, that doesn't necessarily mean that personb
moves as well.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Great, that was a good explanation. Thanks Richard!
-
Hi, I know that objects in javascript, like in many other languages, holds the reference to the object itself and primitive types like number bool holds the actual value. I have something like this:
function manager()
{var anObject = {prop1: 1, prop2: 2};
.
.
.return { getMyObject: function(){ return anObject; }
}
My question is why when I do something like this:
var m = new manager();
var o = m.getMyObject();
o = {a: 10, b: 20};The actual value in 'o' which is a reference type is not changed when I do this:
var o2 = m.getMyObject();
what is actually set in the variable 'o'? and how can I make it persist in the above
constructor of "ype "m" is function manager(){...} m = new constructor (...) m is a pointer ! e.g. new Function(...) creates a pointer of a function new Function() is a member of Javascript predefines types (predefined constructor) so manager() runs like new Function() ? function manager() { var anObject = {prop1: 1, prop2: 2}; // local var !!!! // does not exists after the end of constructor // use global var ! . . . return { getMyObject: function(){ return anObject; } // local function !!!! // does not exists after the end of constructor // use global var to save the pointer. }
-
Your code currently does the following:
- Store the object returned from
getMyObject
in a variable calledo
; - Store a new object in the
o
variable; - Store the object returned from
getMyObject
in a variable calledo2
;
It should be fairly obvious why
o
ando2
do not point to the same object - step 2 makeso
point to a new, different object. If you want your changes to the object to be persisted for future calls of thegetMyObject
function, you need to change the properties of the object returned from that function:var m = new manager();
var o = m.getMyObject();o.prop1 = 10;
o.prop2 = 20;var o2 = m.getMyObject();
// o2 contains { prop1: 10, prop2: 20 }
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
have a look at dis bugga !!! CLT20 2014
CLT20 2014 Live
CLT20 2014 live streaming
CLT20 2014 matches
CLT20 2014 Teams
CLT20 2014 Highlights - Store the object returned from