增加变量名称

发布于 2024-12-09 10:45:22 字数 193 浏览 4 评论 0原文

基本上我想增加变量的名称。执行此操作的正确语法是什么?

for (i=0; i<5; i++) {
    eval("var slider_" + i);

    var slider_+i = function(){
    //some code
}

dojo.addOnLoad(slider_+i);

Basically I want to increment the name of the variable. What is the correct syntax to do this?

for (i=0; i<5; i++) {
    eval("var slider_" + i);

    var slider_+i = function(){
    //some code
}

dojo.addOnLoad(slider_+i);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不即不离 2024-12-16 10:45:22

为什么不直接使用数组呢?

var slider = [];

for (i=0; i<5; i++) {
    slider[i] = function(){
        //some code
    }

    dojo.addOnLoad(slider[i]);
}

或者,您可以根据它们所包含的对象来访问它们。假设它们是全局变量(希望不是):

for (i=0; i<5; i++) {
    window["slider_"+i] = function(){
        //some code
    }

    dojo.addOnLoad(window["slider_"+i]);
}

window["something"] 是访问名为 something 的全局变量的另一种方法。

Why not just use an array?

var slider = [];

for (i=0; i<5; i++) {
    slider[i] = function(){
        //some code
    }

    dojo.addOnLoad(slider[i]);
}

Alternatively, you could access them based on the object they are contained within. Assuming they are global variables (hopefully not):

for (i=0; i<5; i++) {
    window["slider_"+i] = function(){
        //some code
    }

    dojo.addOnLoad(window["slider_"+i]);
}

window["something"] is another way to access a global variable named something.

っ左 2024-12-16 10:45:22

正确的方法是使用对象或数组。这应该有效:

var slider = {}; // object
// var slider = [] ; // array
for (i=0; i<5; i++) {
    slider[i] = function() {
        // some code ...
    }
    dojo.addOnLoad(slider[i]);
}

The right way to do so is to use an object or array. This should work:

var slider = {}; // object
// var slider = [] ; // array
for (i=0; i<5; i++) {
    slider[i] = function() {
        // some code ...
    }
    dojo.addOnLoad(slider[i]);
}
娇俏 2024-12-16 10:45:22

要增加变量名称,您可以将键值数组转换为对象。

  const variablesArray = []

  for (let i = 0; i < 5; i++) {
    variablesArray.push([`key${i}`, `value${i}`])
  }

  let result = Object.fromEntries(variablesArray)

  console.log(result.key0) // return value0
  console.log(result.key1) // return value1
  console.log(result.key2) // return value2
  console.log(result.key3) // return value3
  console.log(result.key4) // return value4

我们使用 Object.fromEntries 返回由属性和方法的键值条目创建的对象

To increment a variable name, you can transform a key-value array into an object.

  const variablesArray = []

  for (let i = 0; i < 5; i++) {
    variablesArray.push([`key${i}`, `value${i}`])
  }

  let result = Object.fromEntries(variablesArray)

  console.log(result.key0) // return value0
  console.log(result.key1) // return value1
  console.log(result.key2) // return value2
  console.log(result.key3) // return value3
  console.log(result.key4) // return value4

We use Object.fromEntries to return an object created by key-value entries for properties and methods

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文