2011-01-21 10 views
0

私は言葉で説明するのが難しいと思っていますので、試してみますがFirefox/firebugがtailspinに入ります。JavaScriptのプロトタイプ関数を使用して 'this'コンテキストの変数を初期化しています

thisthisを参考にしています。私がここでやろうとしているのは、

  1. です。新しいMyObject.Method( 'string'、optionsArray);
  2. optionsArray項目が反復し、プロトタイプの機能セットを(使用して保存されている)

    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をチェックすると助けになると思います。

+1

ここで何をしようとしているのかは分かりません。そのコードは本当にあまり意味がありません。 "Set"関数は、 "Gauge.Speedometer"と呼ばれるもののプロトタイプオブジェクト上にあります。したがって、 "MyObj"または "MyObj.Method"とは何も関係ありません。 – Pointy

+0

@Pointyが修正されました。 – Abhijit

+1

'options'配列が初期化されたり宣言されたりする前に' MyObj.Method'を呼び出すのは、意図的なのか、それとも単にタイプミスですか? – roryf

答えて

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 

注:についてhereを参照してくださいされているものについてはhasOwnProperty

+0

チェーンチップの場合+1:D – fedxc

1

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

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); 
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); 

これは、問題を動作するはずです、あなたのonloadイベントとオプションの外にあなたのこのmyobj =新しいこのmyobj ...を持っている、それがバインドされた匿名関数にプライベート変数として宣言されたとして、その範囲外でしたonloadイベントに変換します。

プロパティの名前を倍にして、値をコピーする方法を修正して、ちょっと混乱させました。