2016-04-07 17 views
4

は、だから私はこの文脈で鍵を参照することは可能ですか?

this.PauseFunctions = { 
     2: { 
      OnSlideTo: function() { 
       console.log("The OnSlideTo function of the event at the 2-second mark was called"); 
      }, 
      OnSlideAway: function() { 
       console.log("The OnSlideAway function of the event at the 2-second mark was called"); 
      } 
     }, 
     5: { 
      OnSlideTo: function() { 
       console.log("The OnSlideTo function of the event at the 5-second mark was called"); 
      }, 
      OnSlideAway: function() { 
       console.log("The OnSlideAway function of the event at the 5-second mark was called"); 
      } 
     } 
    }; 

のようなデータ構造を持っており、その可能なら、私はその値OnSlideTo機能を含むオブジェクトであるキーを参照するために私は思ったんだけど。私のプログラムは、より一般的で保守できるようにkey2ある

 2: { 
      OnSlideTo: function() { 
       console.log("The OnSlideTo function of the event at the " + key + "-second mark was called"); 
      }, 
      OnSlideAway: function() { 
       console.log("The OnSlideAway function of the event at the " key + "-second mark was called"); 
      } 
     } 

にそれを変更する方法がありますたとえば、

 2: { 
      OnSlideTo: function() { 
       console.log("The OnSlideTo function of the event at the 2-second mark was called"); 
      }, 
      OnSlideAway: function() { 
       console.log("The OnSlideAway function of the event at the 2-second mark was called"); 
      } 
     } 

に私は思ったんだけど。

+1

。 –

+0

なぜ、それらの場所にツリー関数が必要ですか? – Amit

答えて

3

確かに値が定義された後にそれを行う必要がありますし、キー値をシードします。あまり複雑ではありません。鍵が入力されると、単純なループになります。各オブジェクトが評価されるように可能ではない

var temp = new function(){ 
 

 
    this.PauseFunctions = { 
 
     2: { 
 
      //Key:, 
 
      OnSlideTo: function() { 
 
       console.log("The OnSlideTo function of the event at the "+this.Key+"-second mark was called"); 
 
      }, 
 
      OnSlideAway: function() { 
 
       console.log("The OnSlideAway function of the event at the "+this.Key+"-second mark was called"); 
 
      } 
 
     }, 
 
     5: { 
 
      //Key:, 
 
      OnSlideTo: function() { 
 
       console.log("The OnSlideTo function of the event at the "+this.Key+"-second mark was called"); 
 
      }, 
 
      OnSlideAway: function() { 
 
       console.log("The OnSlideAway function of the event at the "+this.Key+"-second mark was called"); 
 
      } 
 
     } 
 
    }; 
 

 
    //seed key value 
 
    for(var key in this.PauseFunctions){ 
 
     this.PauseFunctions[key].Key = key; 
 
    } 
 
}; 
 
temp.PauseFunctions[2].OnSlideTo();

関連する問題