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'] 来使用字符串调用数组。
最佳答案
const
或 let
变量不会添加到全局 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/