言葉で説明するのが難しいと感じているので、これが私が試しているコードのスニペットですが、Firefox / firebugはテールスピンに入ります!
私はこれとこれをガイドとして従おうとしています。私がここでやろうとしているのは
- new MyObject.Method('string'、optionsArray);
optionsArrayアイテムは、プロトタイプ関数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; }
そして私のhtmlページのスクリプトブロックで、私は持っています
window.onload = function () { var options = [ { property1: { show: true, min: 0, max: 100 } }, { currentValue: { show: true, colour: 'black' } } ]; var myObj = new MyObj.Method('someDivId',options); }
コードを複雑にしすぎているかどうか教えてください。hasOwnPropertyをチェックすることが役立つと思います。