我可以在这方面提及钥匙吗

Is it possible for me to refer to the key in this context?

本文关键字:钥匙 在这方面 我可以      更新时间:2024-02-22

所以我有一个类似的数据结构

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