Skip to content Skip to sidebar Skip to footer

Does JavaScript Have An Equivalent Of VBScript's ExecuteGlobal?

Is there any alternative of ExecuteGlobal in javascript? Function vbExecuteGlobal(parmSCRIPT) ExecuteGlobal(parmSCRIPT) End Function DevGuru [describes the statement] such: T

Solution 1:

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

See here for details, pros and cons

UPDATE

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

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!

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.

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


Post a Comment for "Does JavaScript Have An Equivalent Of VBScript's ExecuteGlobal?"