将 JavaScript 的 eval() 与多个依赖函数一起使用
Using JavaScript's eval() with multiple dependent functions
如何将动态依赖项加载到 eval()
调用中? (如果可能的话)
我正在尝试 运行 使用多个函数调用 eval()
,但我似乎无法让它工作。即:
function foo() {
return bar;
}
function foo2() {
return foo();
}
理想情况下,我想从事的工作是:
eval("
(function() {
function foo() {
return bar;
}
return foo();
}())
");
但我不断收到 foo is not defined
.
的错误
我知道如果在我页面的html中定义了foo()
,那么我可以用eval()
写一个基本上是foo2()
的函数得到我想要的结果:
eval("
(function() {
return foo();
}())
");
但我希望能够更改这两个函数并且仍然能够调用 foo2()
,它总是引用 foo()
。这可能吗?我可以通过哪些方式使它正常工作?
是的,这是可能的,只需确保您的 eval
ed 代码用新声明覆盖原始函数,通常是在全局范围内。
尝试使用语法
var foo = function(){} 而不是 function foo(){} 在你的 eval 中,我不知道你如何制作多行 js 字符串它不存在,但是一个“;”在 eval
指令之间
尝试在文本区域中输入:
function hello(name) {
alert("hello" + name);
}
那么 func
将是对 hello
(func == hello
) 的引用。然后你可以使用 func
来测试测试用例,就像它是 hello
例如。
var ta = document.getElementById("code");
function run() {
var code = "(function() { return " + ta.value + "; })()"; // code that will return a reference to the function typed by the user
var func = eval(code); // func will be the function the user typed in the textarea
func("me"); // you can call it like you want
}
<textarea id="code"></textarea>
<button onclick="run()">Run</button>
我知道这已经过时了,但我找到了一种方法。你必须在每行的末尾有 \
和转义引号:
var canvas = document.createElement('canvas')
canvas.id = 'dailyTotalUsersCreated';
canvas.setAttribute('class', `bar dailyChart`)
var container = document.querySelector(`#daily`)
container.appendChild(canvas)
eval(` \
var ${canvas.id} = new Chart('${canvas.id}', { \
type: 'bar', \
data: { \
labels: [${lastSevenDays.map(obj => `"${new Date(obj.Date).toDateString()}"`)}], \
datasets: [
{ \
label: 'Total Voice Users Created', \
data: [${lastSevenDays.map(obj => `${obj.Values.filter(obj => { return obj.Name === "Users Created" }).map(obj => totalUsersCreated - obj.Value)}`)}], \
backgroundColor: ['#3e95cd','#8e5ea2','#3cba9f','#e8c3b9','#c45850','#facb48','#384d3f','#247BA0','#F3FFBD','#FF1654'], \
} \
] \
}, \
options: { \
plugins: { \
datalabels: { \
labels: { \
title: { \
color: 'black' \
} \
} \
} \
}, \
scales: { \
yAxes: [{
stacked: false,
gridLines: {
display: true,
color: "rgba(255,99,132,0.2)"
},
ticks: {
beginAtZero: true
}
}]
} \
} \
}); \
`);
如何将动态依赖项加载到 eval()
调用中? (如果可能的话)
我正在尝试 运行 使用多个函数调用 eval()
,但我似乎无法让它工作。即:
function foo() {
return bar;
}
function foo2() {
return foo();
}
理想情况下,我想从事的工作是:
eval("
(function() {
function foo() {
return bar;
}
return foo();
}())
");
但我不断收到 foo is not defined
.
我知道如果在我页面的html中定义了foo()
,那么我可以用eval()
写一个基本上是foo2()
的函数得到我想要的结果:
eval("
(function() {
return foo();
}())
");
但我希望能够更改这两个函数并且仍然能够调用 foo2()
,它总是引用 foo()
。这可能吗?我可以通过哪些方式使它正常工作?
是的,这是可能的,只需确保您的 eval
ed 代码用新声明覆盖原始函数,通常是在全局范围内。
尝试使用语法 var foo = function(){} 而不是 function foo(){} 在你的 eval 中,我不知道你如何制作多行 js 字符串它不存在,但是一个“;”在 eval
指令之间尝试在文本区域中输入:
function hello(name) {
alert("hello" + name);
}
那么 func
将是对 hello
(func == hello
) 的引用。然后你可以使用 func
来测试测试用例,就像它是 hello
例如。
var ta = document.getElementById("code");
function run() {
var code = "(function() { return " + ta.value + "; })()"; // code that will return a reference to the function typed by the user
var func = eval(code); // func will be the function the user typed in the textarea
func("me"); // you can call it like you want
}
<textarea id="code"></textarea>
<button onclick="run()">Run</button>
我知道这已经过时了,但我找到了一种方法。你必须在每行的末尾有 \
和转义引号:
var canvas = document.createElement('canvas')
canvas.id = 'dailyTotalUsersCreated';
canvas.setAttribute('class', `bar dailyChart`)
var container = document.querySelector(`#daily`)
container.appendChild(canvas)
eval(` \
var ${canvas.id} = new Chart('${canvas.id}', { \
type: 'bar', \
data: { \
labels: [${lastSevenDays.map(obj => `"${new Date(obj.Date).toDateString()}"`)}], \
datasets: [
{ \
label: 'Total Voice Users Created', \
data: [${lastSevenDays.map(obj => `${obj.Values.filter(obj => { return obj.Name === "Users Created" }).map(obj => totalUsersCreated - obj.Value)}`)}], \
backgroundColor: ['#3e95cd','#8e5ea2','#3cba9f','#e8c3b9','#c45850','#facb48','#384d3f','#247BA0','#F3FFBD','#FF1654'], \
} \
] \
}, \
options: { \
plugins: { \
datalabels: { \
labels: { \
title: { \
color: 'black' \
} \
} \
} \
}, \
scales: { \
yAxes: [{
stacked: false,
gridLines: {
display: true,
color: "rgba(255,99,132,0.2)"
},
ticks: {
beginAtZero: true
}
}]
} \
} \
}); \
`);