javascript - JS中如何用字符串调用数组名

标签 javascript arrays string

const EleList = [1,2,3]
name = 'Ele'

const render = function(type){
    window[type + 'List'].forEach(function(value){
    console.log("LOL")


render('Ele')

我应该用什么替换 window[name + 'List'] 来使用字符串调用数组。

最佳答案

constlet 变量不会添加到全局 window 对象中。

var 替换 const 应该可以解决您的问题。

var EleList = [1,2,3]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

Global constants do not become properties of the window object, unlike var variables.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

Just like const the let does not create properties of the window object when declared globally (in the top-most scope).

关于javascript - JS中如何用字符串调用数组名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58415620/

相关文章:

JavaScript 字符串和数字转换

string - 遍历并索引文本文件

python - 根据字符串条件删除列

javascript - 如何使用 browserify 和 gulp 输出多个包

javascript - 对 Javascript 数组进行排序

java - 需要帮助按字母顺序对文本文件进行排序

javascript - 如何在这个基于正则表达式的 JSON 到 XML 转换器中处理嵌套数组

javascript - 我可以在 javascript 中使用此方法搜索类吗

javascript - 如何对包含数组的javascript对象进行排序?

javascript - 函数组合 : Augmenting an existing function with additional function in Javascript