相当于 VBScript 的 Execute[Global] 的 Javascript 是 eval()。传递的代码在调用的上下文中进行评估。
See 此处了解详细信息、优点和缺点 https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/eval
UPDATE
不是推荐这种做法,而是澄清我对等效性的理解:
// calling eval in global context is the exact equivalent of ExecuteGlobal
eval("function f0() {print('f0(): yes, we can!');}");
f0();
// calling eval in locally is the exact equivalent of Execute
function eval00() {
eval("function f1() {print('f1(): no, we can not!');}");
f1();
}
eval00();
try {
f1();
}
catch(e) {
print("** error:", e.message);
}
// dirty trick to affect global from local context
function eval01() {
eval("f2 = function () {print('f2(): yes, we can use dirty tricks!');}");
f2();
}
eval01();
f2();
output:
js> load("EvalDemo.js")
f0(): yes, we can!
f1(): no, we can not!
** error: "f1" is not defined.
f2(): yes, we can use dirty tricks!
f2(): yes, we can use dirty tricks!
所以:在 VBScript 中使用 Execute[Global] 可以解决的问题,在 Javascript 中可以使用 eval() 解决;对于某些问题,可能需要额外的工作或技巧。
正如 Abhishek 明确表示的那样“我想在 javascript 中评估 javascript”,我觉得没有必要证明我的答案。