{"id":1066,"date":"2016-10-12T21:31:10","date_gmt":"2016-10-13T02:31:10","guid":{"rendered":"http:\/\/rpchurchill.com\/?p=1066"},"modified":"2017-03-08T23:25:04","modified_gmt":"2017-03-09T04:25:04","slug":"a-simple-discrete-event-simulation-part-24","status":"publish","type":"post","link":"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/10\/12\/a-simple-discrete-event-simulation-part-24\/","title":{"rendered":"A Simple Discrete-Event Simulation: Part 24"},"content":{"rendered":"<p>While making the changes I described <a href=\"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/10\/11\/a-simple-discrete-event-simulation-part-23\/\">yesterday<\/a> I ran into a problem where a long chain of (what I expected to be) references didn&#8217;t return the results I was expecting.  Somewhere in the chain of references something appears to be getting assigned by value.<\/p>\n<p>In order to isolate the chicanery I built up the following test code, which parallels the structure I created when I included the <code>displayGroup<\/code> object in a model component, specifically the arrivals component.  The <code>displayGroup<\/code> object contains an array of <code>displayValue<\/code> objects.  What I see is that none of the indirections work as expected.  If the initial value of a referenced item is changed then the change is not always reflected in the chain of references.  I was originally fooled into thinking it was working because I changed the value of <code>randomFred<\/code> and then called the <code>setFred<\/code> method of the <code>randomObject<\/code>, which actually reassigned the value.  If I change the value of <code>randomFred<\/code> and <em>don&#8217;t<\/em> call the <code>setFred<\/code> method then the pass-through doesn&#8217;t work.<\/p>\n<p>I would know how to control this in a C-style language but JavaScript doesn&#8217;t give the user this type of control.  It does things the way it does things, which in this case means that primitive values (like numbers) are passed by value&#8211;period.  This appears to include primitive values that are properties of objects (or at least closures).  The next trick is to find a workaround.<\/p>\n<p>Let me rephrase that.  I can think of some ugly workarounds but I&#8217;d obviously prefer to find an elegant and efficient one.  <\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n    var randomFred = 3;\r\n    \r\n    function randomObject() {\r\n      this.fred;\r\n      this.setFred = function(fred) {\r\n        this.fred = fred;\r\n      }\r\n      this.getFred = function() {\r\n        return this.fred;\r\n      }\r\n    }\r\n    \r\n    jjj = new randomObject;\r\n    jjj.setFred(randomFred);\r\n    \/\/expect 3\r\n    alert(\"jjj.getFred: \"+jjj.getFred());\r\n\r\n    randomFred = 6;\r\n    jjj.setFred(randomFred);\r\n    \/\/expect 6\r\n    alert(\"jjj.getFred: \"+jjj.getFred());\r\n    \r\n    randomFred = 9;\r\n    \/\/jjj.setFred(randomFred);\r\n    \/\/expect 9, get 6\r\n    alert(\"jjj.getFred: \"+jjj.getFred());\r\n    \r\n    function superObject() {\r\n      this.superFred = 4;\r\n      this.superFred1 = 7;\r\n      this.superRandomObject = new randomObject();\r\n      this.superRandomObject.setFred(this.superFred1);\r\n      this.randomArray = [];\r\n      this.randomArray[0] = new randomObject();\r\n      this.randomArray[0].setFred(randomFred);\r\n    }\r\n    \r\n    var super1 = new superObject;\r\n    \/\/expect 7\r\n    alert(\"super1.superRandomObject.getFred: \"+super1.superRandomObject.getFred());\r\n\r\n    super1.superRandomObject.setFred(randomFred);\r\n    \/\/expect 6\r\n    alert(\"super1.superRandomObject.getFred: \"+super1.superRandomObject.getFred());\r\n\r\n    super1.superRandomObject.setFred(super1.superFred);\r\n    \/\/expect 4\r\n    alert(\"super1.superRandomObject.getFred: \"+super1.superRandomObject.getFred());\r\n    \r\n    super1.superFred = 13;\r\n    \/\/expect 13, get 4\r\n    alert(\"super1.superRandomObject.getFred: \"+super1.superRandomObject.getFred());\r\n\r\n    randomFred = 12;\r\n    \/\/expect 12, get 6\r\n    alert(\"super1.randomArray[0].getFred(): \"+super1.randomArray[0].getFred());\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>While making the changes I described yesterday I ran into a problem where a long chain of (what I expected to be) references didn&#8217;t return the results I was expecting. Somewhere in the chain of references something appears to be &hellip; <a href=\"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/10\/12\/a-simple-discrete-event-simulation-part-24\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[60],"tags":[121,49],"_links":{"self":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/1066"}],"collection":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1066"}],"version-history":[{"count":7,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/1066\/revisions"}],"predecessor-version":[{"id":1711,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/1066\/revisions\/1711"}],"wp:attachment":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1066"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}