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!
I'm trying to follow this and this as a guide. What I'm trying to do here is
- new MyObject.Method('string',optionsArray);
optionsArray items are iterated and saved using the prototype function Set()
if(typeof(MyObj) == 'undefined') MyObj= {}; MyObj.Method = function initialise(id,options) { this.id = id; this.options = options; this.properties ={}; for (var i = 0; i < this.options.length; i++) // =>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); //=> correctly prints 'property1' and 'currentValue' this.Set(keys,obj); //=> this is i guess where it enters a loop? } } } //sets properties MyObj.Method.prototype.Set = function (name, value) { this.properties[name.toLowerCase()] = value; }and in my html page script block, i have
window.onload = function () { var options = [ { property1: { show: true, min: 0, max: 100 } }, { currentValue: { show: true, colour: 'black' } } ]; var myObj = new MyObj.Method('someDivId',options); }
please advise if I'm over complicating the code. I think checking for hasOwnProperty would help.
MyObj.Methodbefore theoptionsarray is initialised or even declared, was this intentional or just a typo?MyObjat all, so the first line of that snippet will throw an error.MyObj.Methodcall will execute immediately, while theoptionsarray won't be created until the window.onload event is fired.