Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript object/namespaces - how to access properties added from nested functions (example included)
    primarykey
    data
    text
    <p>I've been working on understanding Javascript objects and namespaces and I am a little confused by how we can generate/display object using nested functions. I don't understand how I can nest functions to basically create object factories and display them in the page. Example below.</p> <p>I have tried RTFG and some of the examples on this site, but nothing seems applicable. Any links to relevant Q&amp;A appreciated. but I am hoping an experienced person can explain this behavior.</p> <p>The below three files constitute a simple app to load an index.html page with three car entries, generated by calling a getCar() function three times and setting custom attributes, then adding an engine using a function which itself uses a (completely wrong) function to calculate horsepower. </p> <p>I'd expect to be able to access properties of the returned car1, car2, car3 objects in the HTML but when I load index.html from below in a browser, I see no errors in the Javascript but I don't expect these results:</p> <pre><code>1. undefined &lt;-- this is defined, I set the name in the call from the updatePage() function 2. [object Object] &lt;-- the name is not an object, it's a primitive 3. {"name":"","engine":"","wheels":"","options":"none"} &lt;-- this has properties set, why not show them? </code></pre> <p>Also understanding if I use namespaces, one for car and one for engine, would I need to declare them both at the top of each file like so:</p> <pre><code>var car = car || {} var engine = engine || {} </code></pre> <p>and then reference the calls from carFactory.js with:</p> <pre><code>engine.getEngine(); </code></pre> <p>Like I said, appreciate any help. I am just trying to understand, this is not a class assignment. I have looked at the sections in JavaScript sixth edition and can't see how this namespacing/object setup would work in practice. Apprecate any help.</p> <p>index.html</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt; &lt;title&gt;Object example&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="input"&gt; &lt;p id='intro'&gt;Object-Namespace Example&lt;/p&gt; &lt;input type='button' onclick='updatePage()' value='New Car'/&gt; &lt;/div&gt; &lt;div id="cars"&gt; &lt;div id="car1"&gt;car1&lt;/div&gt; &lt;div id="car2"&gt;car2&lt;/div&gt; &lt;div id="car3"&gt;car3&lt;/div&gt; &lt;/div&gt; &lt;div id="jslib"&gt; &lt;script type="text/javascript" src="carFactory.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="engineFactory.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function updatePage() { var car1 = getNewCar("Dodge"); var oldHTML = document.getElementById('car1').innerHTML; var newHTML = "&lt;span&gt;" + car1["name"] + "&lt;/span&gt;"; document.getElementById('car1').innerHTML = newHTML; var car2 = getNewCar("Toyota"); var oldHTML = document.getElementById('car2').innerHTML; var newHTML = "&lt;span&gt;" + car2 + "&lt;/span&gt;"; document.getElementById('car2').innerHTML = newHTML; var car3 = getNewCar("Hudson"); var oldHTML = document.getElementById('car3').innerHTML; var newHTML = "&lt;span&gt;" + JSON.stringify(car3) + "&lt;/span&gt;"; document.getElementById('car3').innerHTML = newHTML; }; &lt;/script&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>carFactory.js</p> <pre><code>function getNewCar(dName) { this.retValue=""; var theCar = new car; theCar.setProp("name",dName); theCar.setProp("engine",getEngine()); theCar.setProp("wheels",4); return theCar; }; car = function() { this.name=""; this.engine=""; this.wheels=""; this.options="none"; this.setProp=function(prop,val) { this[prop]=val; } }; </code></pre> <p>engineFactory.js</p> <pre><code>getEngine = function() { var theEngine = new engine(); theEngine.setProp["cylinders"] = 8; theEngine.setProp["capacity"]=400; theEngine.setProp["fuel"]="injected"; return theEngine; }; engine = function() { this.capacity=""; this.fuel=""; this.cylinder=""; this.nitrous="no"; this.horsepower=getHorsepower(this); this.setProp=function(prop,val) { this[prop]=val; } } function getHorsepower(engine) { this.retValue=0; this.retValue=engine.cylinder*engine.capacity; return this.retValue; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload