2016-04-07 50 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函数的对象的关键。例如,在

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

我不知道是否有一种方法将其更改为

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

其中key2使我的程序可以更加普遍和维护。

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

相关问题