gpt4 book ai didi

javascript - 在这种情况下,我可以引用 key 吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:57:21 24 4
gpt4 key购买 nike

所以我有这样的数据结构

    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 这样我的程序可以更通用和可维护。

最佳答案

当然可以,但是您必须在定义值之后执行此操作,然后为键值设置种子。它不应该太复杂,只是一个简单的 for 循环,一旦键被填充。

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

关于javascript - 在这种情况下,我可以引用 key 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36486621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com