R
Roman Ziak
Hello,
there were times when I used to be looking for a way to access
JavaScript Global object similar to those found in VBScript or PHP
($GLOBALS). At present this has only academic value for me. I was doing
research on JavaScript inheritance recently (simplifying it in
particular) and after reading 10.1.1, 10.1.3 and some other sections of
ECMA262 [1] I got a hint on accessing global object from different than
global scope.
Below is my example (please note that the host is WSH, feel free to
change WScript.Echo to your favourite output call):
myGlobal = this;
function AddVar (id, val)
{
myGlobal[id] = val;
}
function EvalCode (code)
{
myGlobal.eval(code);
}
AddVar("roman", 100.1);
WScript.Echo("roman=" + roman);
EvalCode("function TestEval1 (x,y) { return x+y; }")
// this fails
//WScript.Echo( "TestEval1(150,100) = ", TestEval1(150,100) );
EvalCode("TestEval2 = function (x,y) { return x-y; }")
// this passes
WScript.Echo( "TestEval2(150,100) = ", TestEval2(150,100) );
function EnumProp (o)
{
for(var k in o)
WScript.Echo(k + " : " + typeof(o[k]));
}
EnumProp(myGlobal); // same result with "this"
Function AddVar() works properly - the global variable is created. This
is, however, quite useless, because same behaviour can be invoked in
simpler way.
Function EvalCode() will not work for function declaration, but it will
work for assigning a function literal to new (global) variable (which is
variable rather than function definition).
EnumProp() is used to enumerate all defined variables and shows that
TestEval2() has been defined, however TestEval1() is missing, although
the code execute without error.
My question - anybody can explain where is function TestEval1() and why
it did not become property in intrisic Global object ?
Thank you
Roman
there were times when I used to be looking for a way to access
JavaScript Global object similar to those found in VBScript or PHP
($GLOBALS). At present this has only academic value for me. I was doing
research on JavaScript inheritance recently (simplifying it in
particular) and after reading 10.1.1, 10.1.3 and some other sections of
ECMA262 [1] I got a hint on accessing global object from different than
global scope.
Below is my example (please note that the host is WSH, feel free to
change WScript.Echo to your favourite output call):
myGlobal = this;
function AddVar (id, val)
{
myGlobal[id] = val;
}
function EvalCode (code)
{
myGlobal.eval(code);
}
AddVar("roman", 100.1);
WScript.Echo("roman=" + roman);
EvalCode("function TestEval1 (x,y) { return x+y; }")
// this fails
//WScript.Echo( "TestEval1(150,100) = ", TestEval1(150,100) );
EvalCode("TestEval2 = function (x,y) { return x-y; }")
// this passes
WScript.Echo( "TestEval2(150,100) = ", TestEval2(150,100) );
function EnumProp (o)
{
for(var k in o)
WScript.Echo(k + " : " + typeof(o[k]));
}
EnumProp(myGlobal); // same result with "this"
Function AddVar() works properly - the global variable is created. This
is, however, quite useless, because same behaviour can be invoked in
simpler way.
Function EvalCode() will not work for function declaration, but it will
work for assigning a function literal to new (global) variable (which is
variable rather than function definition).
EnumProp() is used to enumerate all defined variables and shows that
TestEval2() has been defined, however TestEval1() is missing, although
the code execute without error.
My question - anybody can explain where is function TestEval1() and why
it did not become property in intrisic Global object ?
Thank you
Roman