我有一个这样的字符串(HTML select list):
const config = {
"celsius": {
"celsius": v => v,
"fahrenheit": v => v * 1.8 + 32,
"kelvin": v => v + 273.15,
"rankine": v => (v + 273.15) * 1.8,
"reaumur": v => v * 0.8,
},
// more...
}
我想从输出(operationPlace)
中截取前5个字符var listOperation = config[listFromV][listToV].substring(5);
document.getElementById("operationPlace").innerHTML = listOperation;
// now it looks like this: v => v or v => v * 1.8 + 32 etc.
// should look like this: v or v * 1.8 + 32 etc. (I'd like to change that "v" to numbers from the input (inputPlace) too)
我尝试使用子字符串,但它不起作用。有办法做到这一点吗?
你可以通过调用.toString
得到一个字符串形式的函数定义,然后调用.substring
去掉函数签名部分:
var listOperation = config[listFromV][listToV];
document.getElementById("operationPlace").innerHTML = listOperation.toString().substring(5);
请注意,这种方法很幼稚,只适用于签名由单个1个字符长的参数和没有括号的箭头符号组成的函数。