首页 > 解决方案 > 使用函数的参数名称作为字符串

问题描述

如何将函数的参数名称作为字符串传递给嵌套函数?我有 2 个变量,假设 name1=5 和 name2=10,我想创建一个函数,该函数将使用其中一个变量值,而在其他地方使用变量名作为字符串。

var name1 = 5
var name2 = 10

function func(arg){
    document.getElementById("a1").innerText = arg //that works fine, it sets the text as 5 or 10
    document.getElementById("arg" + "some string").innerText = "some other string" // here i need the argument's name as string, for example "name1".
}

我希望我的问题可以理解,英语不是我的默认语言。

标签: javascript

解决方案


推荐阅读