javascript - What is difference between assigning value to variable and storing value into variable -
i reading book object oriented javascript , found this:
reference types not store object directly variable assigned, object variable in example doesn’t contain object instance. instead, holds pointer (or reference) location in memory object exists. primary difference between objects , primitive values, primitive stored directly in variable.
my question meaning of ?
"reference types not store object directly variable assigned, object variable in example doesn’t contain object instance." ??
in image provided can see
var object1 = new object(); var object2 = object1;
in case, have 2 variables both store reference (think of pointer) place in memory. in place object stored. if change object via 1 of references, , access via other 1 see has changed.
object1.somevariable = 'asdf'; object2.somevariable = 'newvalue'; console.log(object1.somevariable); // give 'newvalue' console.log(object2.somevariable); // give 'newvalue'
if have scalar values, not store references, store value itself.
think of example:
var primitivestring = 'asdf'; var anotherprimitivestring = primitivestring;
since both store value self, can change 1 of 2 strings, other 1 still contain asdf
, since not reference something.
anotherprimitivestring = 'newvalue'; console.log(primitivestring); // give 'asdf' console.log(anotherprimitivestring); // give 'newvalue'
here have jsfiddle explained example.
Comments
Post a Comment