例如,如果我有
var x = "console";
是否可以运行
x.log(“ Hello”); 在JavaScript中? 我使用window和eval来运行它,但是它不起作用或者有使用它们的特定方法吗?
答案 0 :(得分:0)
以下是使用字符串为控制台调用原始函数的两种方法。
eval("console").log("hello world");
window["console"].log("hello world");
如果字符串是“ log”,则将按照以下方式工作。
console["log"]("hello world");
如果只想使用字符串,则可以执行以下操作:
eval("console")["log"]("hello world");
window["console"]["log"]("hello world");
eval("console.log('hello world')");