Javascript 我是否可以在此上下文中引用密钥?
所以我有一个数据结构,比如Javascript 我是否可以在此上下文中引用密钥?,javascript,Javascript,所以我有一个数据结构,比如 this.PauseFunctions = { 2: { OnSlideTo: function () { console.log("The OnSlideTo function of the event at the 2-second mark was called"); }, OnSlideAway: function () {
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");
}
}
其中,
key
是2
,这样我的程序就可以更通用、更易于维护。当然可以,但您必须在定义了值之后进行操作,然后为键值设定种子。它不应该太复杂,只要在填充键后执行一个简单的for循环即可
var-temp=new函数(){
this.PauseFunctions={
2: {
//关键字:,
OnSlideTo:函数(){
log(“调用了“+this.Key+”-第二个标记处事件的OnSlideTo函数”);
},
OnSlideAway:函数(){
log(“调用了“+this.Key+”-第二个标记处事件的OnSlideAway函数”);
}
},
5: {
//关键字:,
OnSlideTo:函数(){
log(“调用了“+this.Key+”-第二个标记处事件的OnSlideTo函数”);
},
OnSlideAway:函数(){
log(“调用了“+this.Key+”-第二个标记处事件的OnSlideAway函数”);
}
}
};
//种子键值
for(此.PauseFunctions中的var键){
this.PauseFunctions[key].key=key;
}
};
temp.PauseFunctions[2].OnSlideTo()代码>这是不可能的,因为每个对象的求值方式不同。为什么需要树函数呢?