Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing javascript prototype function to initialise variable in 'this' context
    text
    copied!<p>I'm finding it difficult to explain in words, so here's a snippet of code I'm trying out but Firefox/firebug goes into tailspin!</p> <p>I'm trying to follow <a href="http://www.rgraph.net/libraries/RGraph.bar.js" rel="nofollow">this</a> and <a href="https://sites.google.com/site/abhijitchanda/files/gauge.js" rel="nofollow">this</a> as a guide. What I'm trying to do here is </p> <ol> <li>new MyObject.Method('string',optionsArray);</li> <li><p>optionsArray items are iterated and saved using the prototype function Set()</p> <pre><code>if(typeof(MyObj) == 'undefined') MyObj= {}; MyObj.Method = function initialise(id,options) { this.id = id; this.options = options; this.properties ={}; for (var i = 0; i &lt; this.options.length; i++) // =&gt;options.length=2 (correct) { var obj = this.options[i]; //get the keynames, pass with values to Set() to update properties for (var keys in obj) { console.log(keys); //=&gt; correctly prints 'property1' and 'currentValue' this.Set(keys,obj); //=&gt; this is i guess where it enters a loop? } } } //sets properties MyObj.Method.prototype.Set = function (name, value) { this.properties[name.toLowerCase()] = value; } </code></pre> <p>and in my html page script block, i have </p> <pre><code>window.onload = function () { var options = [ { property1: { show: true, min: 0, max: 100 } }, { currentValue: { show: true, colour: 'black' } } ]; var myObj = new MyObj.Method('someDivId',options); } </code></pre></li> </ol> <p>please advise if I'm over complicating the code. I think checking for hasOwnProperty would help.</p>
 

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