JavaScript 是否具有与 VBScript 的 ExecuteGlobal 等效的功能? [英] Does JavaScript have an equivalent of VBScript's ExecuteGlobal?

查看:31
本文介绍了JavaScript 是否具有与 VBScript 的 ExecuteGlobal 等效的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javascript 中是否有 ExecuteGlobal 的替代方案?

Is there any alternative of ExecuteGlobal in javascript?

Function vbExecuteGlobal(parmSCRIPT)
    ExecuteGlobal(parmSCRIPT)
End Function

DevGuru [描述声明] 如下:

DevGuru [describes the statement] such:

ExecuteGlobal 语句采用单个字符串参数,将其解释为 VBScript 语句或语句序列,并在全局命名空间中执行这些语句.

The ExecuteGlobal statement takes a single string argument, interprets it as a VBScript statement or sequence of statements, and executes these statements in the global namespace.

推荐答案

与 VBScript 的 Execute[Global] 等效的 Javascript 是 eval().传递的代码在调用的上下文中进行评估.

The Javascript equivalent to VBScript's Execute[Global] is eval(). The passed code is evaluated in the context of the call.

请参阅此处了解详情、优缺点

更新

不是推荐这种做法,而是澄清我对等价的理解:

Not to recommend such practices, but to clarify my understanding of equivalence:

// 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();

输出:

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()可以解决;对于某些问题,可能需要额外的工作或技巧.

So: Problems that can be solved using Execute[Global] in VBScript can be solved using eval() in Javascript; for some problems extra work or tricks may be necessary.

正如 Abhishek 明确表示我想在 javascript 中评估 javascript"一样,我觉得没有必要证明我的回答是正确的.

As Abhishek explicitly said "i want to evaluate javascript in javascript", I don't feel the need to justify my answer.

这篇关于JavaScript 是否具有与 VBScript 的 ExecuteGlobal 等效的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆