{"id":6423,"date":"2014-04-17T23:18:22","date_gmt":"2014-04-17T23:18:22","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/17\/js-if-variable-gets-object-from-a-function-doas-the-variable-stores-the-content-of-the-object-or-a-link-to-the-object-collection-of-common-programming-errors\/"},"modified":"2014-04-17T23:18:22","modified_gmt":"2014-04-17T23:18:22","slug":"js-if-variable-gets-object-from-a-function-doas-the-variable-stores-the-content-of-the-object-or-a-link-to-the-object-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/17\/js-if-variable-gets-object-from-a-function-doas-the-variable-stores-the-content-of-the-object-or-a-link-to-the-object-collection-of-common-programming-errors\/","title":{"rendered":"JS: If variable gets object from a function, doas the variable stores the content of the object or a link to the object?-Collection of common programming errors"},"content":{"rendered":"<p>I am new to Java Script and I don&#8217;t understand, what a variable is storing, if it gets the return value of a function that is a object. My fear is, that such a variable is not storing the value, but only a link to a memory, that is maybe overwritten a little later.<\/p>\n<h2>I tried this to find it out:<\/h2>\n<p>This function returns a object with the content: name=&#8221;first&#8221; or name=&#8221;second&#8221;:<\/p>\n<pre><code>var returnObject = function (theSecond) {\n    var obj = { name : \"first\" };\n    if (theSecond) {\n        obj.name = 'second';\n    }\n    return obj;\n};\n<\/code><\/pre>\n<p>2 variables are now receiving, what the function returns:<\/p>\n<pre><code>var obj1 = returnObject(false); \/\/ obj3.name==first\nvar obj2 = returnObject(true);  \/\/ obj2.name==second\n<\/code><\/pre>\n<p><strong>Do the variables store copys of the object that is in the function, or do they link to the objecs in the function?<\/strong> If it is only a link, they are linking to a cleared memory, that can be unexpected overwritten. Therefor I hope, that they are storing copys of the object.<\/p>\n<p><strong>Is obj3 only a link to ojb1?<\/strong><\/p>\n<pre><code>var obj3 = obj1;        \/\/ obj3 is a link to obj1?\n<\/code><\/pre>\n<p>It seems, that obj3 is not storing a copy of obj1 but only a link to obj1 because:<\/p>\n<pre><code>obj3.name = \"third\";    \/\/ obj3.name==third and immediately obj1.name==third\n<\/code><\/pre>\n<p>Ok, the variable obj1 shows now the same as the variable obj3, because it is a link to obj3.<\/p>\n<h2><strong>Confusing:<\/strong><\/h2>\n<pre><code>obj1 = returnObject(false); \/\/ obj1.name==first BUT still obj3.name==third. WHY???\n<\/code><\/pre>\n<p>Why is the link broken now? obj3.name is still &#8220;third&#8221; but obj1.name is &#8220;first&#8221;!? How can obj3 store a different value as obj1?<\/p>\n<h2><strong>A copy function?<\/strong><\/h2>\n<pre><code>var copy = function (obj) { return obj; };\n<\/code><\/pre>\n<p>Using it:<\/p>\n<pre><code>obj2 = copy(obj1);      \/\/ obj2.name==first\n<\/code><\/pre>\n<p>Is obj2 now a copy of obj1, or a link to obj1?<\/p>\n<pre><code>obj2.name = \"secondAgain\";  \n<\/code><\/pre>\n<p>obj2.name is now secondAgain but immediately obj1.name is also secondAgain. It seems, that it is not a copy function. obj2 is only a link to obj1. \ud83d\ude41<\/p>\n<p>Now I am a confused JavaScript beginner. :-((<\/p>\n<p>Tags: javascript object copy function link return-value pointer clone assign assigning<\/p>\n<h2>UPDATE:<\/h2>\n<p>I understand the 2 answers that:<\/p>\n<pre><code>var retObj = function { return { name : \"first\" }; };\nvar objA = retObj();\n<\/code><\/pre>\n<p>&#8230; has the same effect like:<\/p>\n<pre><code>var objA = { name : \"first\" };\n<\/code><\/pre>\n<p>And:<\/p>\n<pre><code>var copy = function (obj) { return obj; };\nvar objB = copy(objA);\n<\/code><\/pre>\n<p>&#8230; has the same effect like:<\/p>\n<pre><code>var objB = objA;\n<\/code><\/pre>\n<h2>But why:<\/h2>\n<p>&#8230; can a variable, that is a reference to a other variable, stores a individual object?<\/p>\n<pre><code>var retObjX  = function () { return { name : \"X\"  }; };\nvar retObjX2 = function () { return { name : \"X2\" }; };\nvar objX = retObjX();   \/\/ objX=X\nvar objY = objX;        \/\/ Y refers X\nobjX = retObjX2();      \/\/ objX=X2 BUT! objY=X\n<\/code><\/pre>\n<p>I think, that the variable objY ist not a reference to the variable objX, but a reference to the objext in retObjX() because: objY shows still X not X2.<\/p>\n<p>What, if the object, that a function returns, is not a constant literal object, but a object that is build dynamically? Would a reference variable show the object that was build the last time by this function?<\/p>\n<pre><code>var retObjDyn = function () { return { rnd : Math.floor(Math.random() * 100) }; };\nvar objDynX = retObjDyn();  \/\/ 44\nvar objDynY = objDynX;      \/\/ 44\nobjDynX = retObjDyn();      \/\/ objDynX=78 BUT STILL: objDynY=44\n<\/code><\/pre>\n<p>objDynY that is a reference to the object that was stored in objDynX, shows still this object, even when objDynX shos a different object.<\/p>\n<p>Now I think, that retObjDyn() builds a new object at every invoke. <strong>But are these objects save, or is the memory released for other things?<\/strong> Than objDynY can change its content unexpected.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am new to Java Script and I don&#8217;t understand, what a variable is storing, if it gets the return value of a function that is a object. My fear is, that such a variable is not storing the value, but only a link to a memory, that is maybe overwritten a little later. I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6423","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6423"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6423\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}