JavaScript全局对象 [英] JavaScript global object

查看:73
本文介绍了JavaScript全局对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


有时我曾经在寻找一种方法来访问

JavaScript Global对象类似于VBScript或PHP中的对象

($ GLOBALS)。目前这对我来说只具有学术价值。我最近正在进行关于JavaScript继承的研究(特别是简单地将它简化为

)以及阅读10.1.1,10.1.3和其他一些部分的

ECMA262 [1]我得到了一个关于从全球范围内获取全球对象的提示。


以下是我的例子(请注意,主持人是WSH,随时可以将
改成WScript.Echo到你最喜欢的输出调用):

myGlobal = this;


函数AddVar(id,val)

{

myGlobal [id] = val;

}


函数EvalCode(代码)

{

myGlobal.eval(代码);

}


AddVar(" roman",100.1);


WScript.Echo(" roman =" + roman);


EvalCode(" function TestEval1(x,y){return x + y;}")


//这个失败

//WScript.Echo (TestEval1(150,100)=,TestEval1(150,100));


EvalCode(&te; stEval2 = function(x,y){return x-y; }")


//这传递

WScript.Echo(" TestEval2(150,100)=",TestEval2(150,100));


函数EnumProp(o)

{

for(var k in o)

WScript.Echo (k +":" + typeof(o [k]));

}


EnumProp(myGlobal); //与this相同的结果

函数AddVar()正常工作 - 创建全局变量。然而,这个

是没用的,因为可以用更简单的方式调用相同的行为。


函数EvalCode()不适用于函数声明,但它会将函数文本分配给新的(全局)变量(这是一个变量而不是函数定义)。


EnumProp()用于枚举所有已定义的变量并显示

TestEval2()已定义,但是缺少TestEval1(),尽管

代码执行没有错误。


我的问题 - 任何人都可以解释函数TestEval1()以及为什么

它没有成为属性内在的全球对象?


谢谢

罗马

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

推荐答案

GLOBALS) 。目前这对我来说只具有学术价值。我最近正在进行关于JavaScript继承的研究(特别是简单地将它简化为

)以及阅读10.1.1,10.1.3和其他一些部分的

ECMA262 [1]我得到了一个关于从全球范围内获取全球对象的提示。


以下是我的例子(请注意,主持人是WSH,随时可以将
改成WScript.Echo到你最喜欢的输出调用):

myGlobal = this;


函数AddVar(id,val)

{

myGlobal [id] = val;

}


函数EvalCode(代码)

{

myGlobal.eval(代码);

}


AddVar(" roman",100.1);


WScript.Echo(" roman =" + roman);


EvalCode(" function TestEval1(x,y){return x + y;}")


//这个失败

//WScript.Echo (TestEval1(150,100)=,TestEval1(150,100));


EvalCode(&te; stEval2 = function(x,y){return x-y; }")


//这传递

WScript.Echo(" TestEval2(150,100)=",TestEval2(150,100));


函数EnumProp(o)

{

for(var k in o)

WScript.Echo (k +":" + typeof(o [k]));

}


EnumProp(myGlobal); //与this相同的结果

函数AddVar()正常工作 - 创建全局变量。然而,这个

是没用的,因为可以用更简单的方式调用相同的行为。


函数EvalCode()不适用于函数声明,但它会将函数文本分配给新的(全局)变量(这是一个变量而不是函数定义)。


EnumProp()用于枚举所有已定义的变量并显示

TestEval2()已定义,但是缺少TestEval1(),尽管

代码执行没有错误。


我的问题 - 任何人都可以解释函数TestEval1()以及为什么

它没有成为属性内在的全球对象?


谢谢

Roman
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


Roman Ziak写道:

....
Roman Ziak wrote:
....
EnumProp()用于枚举所有已定义的变量,并显示已定义了TestEval2(),但缺少TestEval1(),尽管
我的问题 - 任何人都可以解释函数TestEval1()以及为什么
它没有成为intrisic Global对象的属性?
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 ?




也许你会在以下网址找到一些相关的读物:
http://blogs.msdn.com/ericlippert/ar...04/414684.aspx
http://blogs.msdn.com/ericlippert/ar ... .30 /475826.aspx


来自维也纳的Csaba Gabor



Perhaps you will find some relevant reading at:
http://blogs.msdn.com/ericlippert/ar...04/414684.aspx and
http://blogs.msdn.com/ericlippert/ar...30/475826.aspx

Csaba Gabor from Vienna


Roman Ziak于27/03说/ 2006上午9:18 AEST:
Roman Ziak said on 27/03/2006 9:18 AM AEST:
你好,

有时候我曾经在寻找一种方法来访问类似于那些的JavaScript Global对象在VBScript或PHP中找到
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
(


这篇关于JavaScript全局对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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