0

言葉で説明するのが難しいと感じているので、これが私が試しているコードのスニペットですが、Firefox / firebugはテールスピンに入ります!

私はこれこれをガイドとして従おうとしています。私がここでやろうとしているのは

  1. new MyObject.Method('string'、optionsArray);
  2. 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をチェックすることが役立つと思います。

4

3 に答える 3

3

これは、必要なことを達成するためのよりクリーンな方法である必要があります。

function MyObj(id, options) { // a function that will get used as the constructor
    this.id = id;
    this.options = options;
    this.properties = {};
    this.set(options); // call the set method from the prototype
}

MyObj.prototype.set = function(options) { // set the options here
    for(var i = 0, l = options.length; i < l; i++) {
        var obj = this.options[i];
        for(var key in obj) {
            if (obj.hasOwnProperty(key)) { // this will exclude stuff that's on the prototype chain!
                this.properties[key] = obj[key];
            }
        }
    }
    return this; // return the object for chaining purposes
                 // so one can do FooObj.set([...]).set([...]);
};

var test = new MyObj('simeDivId', [...]); // create a new instance of MyObj
test.set('bla', [...]); // set some additional options

注:詳細についてhasOwnPropertyは、こちらをご覧ください

于 2011-01-21T14:40:43.717 に答える
1

この関数がのプロパティであると明らかに宣言しているのでMyObj、関数名を宣言して削除しました。最終的なコードは次のようになり、それは私にとっては問題なく実行されます。オブジェクトには関数の概念がないため、プロトタイプ関数を宣言するまで実際に関数を呼び出すことはできないことに注意してください。initialiseMyObjSet

var MyObj = {};

MyObj.Method = function (id,options)
{
    this.id = id;
    this.properties ={};

    for (var i = 0; i < options.length; i++)  // =>options.length=2 (correct)
    {
        var obj = 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?
        }
    }
}

MyObj.Method.prototype.Set = function (name, value) 
{
    this.properties[name.toLowerCase()] = value;
}

var options = [
    {    property1: {
            show: true,
            min: 0,
            max: 100
        }
    },
    {
        currentValue: {
            show: true,
            colour: 'black'
        }
    }
];

var myObj = new MyObj.Method('someDivId',options);
于 2011-01-21T13:46:18.380 に答える
1
var MyObj = {};

MyObj.Method = function initialise(id,options) {

    this.id = id;
    this.options = options;
    this.properties = {};

    for (var i = 0; i < this.options.length; i++)
    {
        var obj = this.options[i];  
        for (var keys in obj) {

            this.Set(keys,obj[keys]); 

            //*fix obj => obj[keys] 
            // (and it should be singular key rather then keys

        }
    }

    console.log(this.properties) // will output what you want
}

//sets properties
MyObj.Method.prototype.Set = function (name, value) {
    this.properties[name.toLowerCase()] = value;
}


var options = [{
    property1: {
        show: true,
        min: 0,
        max: 100
    }
},{
    currentValue: {
        show: true,
        colour: 'black'
    }
}];

var myObj = new MyObj.Method('someDivId',options);

これは問題なく機能するはずです。myObj=newMyObj ...がonloadイベントの外にあり、onloadイベントにバインドされた無名関数のプライベート変数として宣言されているため、オプションがスコープ外でした。

プロパティの名前が2倍になり、少し面倒になるため、値をプロパティにコピーする方法も修正しました。

于 2011-01-21T13:46:35.067 に答える