奇怪的变量范围 [英] Weird variable scope

查看:64
本文介绍了奇怪的变量范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上周末,我参加了一场关于JavaScript的会议,在No Fluff

Just Stuff会议上学到了一个有趣的怪癖我想要

问一个关于..的问题。


请使用以下代码:


x = 5;


function foo(){

alert(x);

var x = 10;

alert(x);

}


foo();


第一个警报将显示''undefined'',第二个警告将显示

10。显然,JavaScript甚至在达到该行之前就识别出foo()内部的''var x = 10''声明

- 它会发现

声明,但不是初始化为10.只有在x被设置为

10并且达到第二个警报后才会显示10个。


我的问题是......有没有从这段代码中获取foo()中的全局

x变量(5)的方法?

解决方案



Joe Attardi写道:

上周末,我参加了No Fluff
Just Stuff会议上的JavaScript会议,并学到了一个有趣的怪癖,我想问问
关于这个问题的答案。

拿这段代码:

x = 5;

函数foo(){
alert(x) ;
var x = 10;
alert(x);
}

foo();

第一个警告将显示' 'undefined'',第二个将显示
10.显然JavaScript在foo()内部识别'var x = 10''声明
甚至在该行到达之前 - 它发现
声明,但不是初始化为10.只有在x设置为
10并且达到第二个警报后才会显示10。

我的问题是......有没有从这段代码中获取foo()中的全局
x变量(5)的方法?




请尝试以下方法:

alert(window.x);





alert(窗口[" x"]);


Joe Attardi写道:< blockquote class =post_quotes>上周末,我参加了No Fluff
Just Stuff会议上的JavaScript会议,并学到了一个有趣的怪癖,我想问一个关于......的问题。

取这段代码:

x = 5;

函数foo(){
alert(x);
var x = 10 ;
alert(x);
}

foo();

第一个警报将显示''undefined'',第二个警告将显示显示
10.显然JavaScript甚至在达到该行之前就识别foo()内的'var x = 10''声明 - 它会发现
声明,但不会初始化为10 。


否 - 它返回x未定义。也就是说,''x''尚未定义为
。您可以在以下行中定义它。

只有在x设置为
10并且达到第二个警报后才会显示10。


因为现在已经定义了x。

我的问题是......有没有办法从这段代码访问全局
x变量(5)来自foo()?




将全局x变量声明为:

var x = 5;


2006年3月15日21:44,Joe Attardi写道:


[snip]

x = 5;


所有变量,包括全局变量,都应使用

var语句显式声明。

function foo(){
alert(x);
var x = 10;
alert(x);
}

foo();

第一个警报将显示''undefined'',第二个警告将显示
10.显然,即使在达到该行之前,JavaScript也会在foo()内识别出''var x = 10''声明
- 它会发现
声明,但不是初始化为10.只有在x设置为
10并且达到第二个警报后才会显示10。


变量实例化在代码执行开始之前发生,如10.1.3变量实例化,ECMA-262第3版[1]中所描述的
>

简单地说,当调用foo函数时,正式参数,函数

声明和变量声明被处理以创建本地

变量。之后,执行开始于变量初始化

在评估分配时发生。

我的问题是......是否有任何方法可以从此代码访问全局
foo()中的x变量(5)?




是的,通过访问全局变量作为全局对象的属性。

使用代码原样,可以使用this运算符来引用

全局对象:


var x = 5;


函数foo(){

alert(this.x); // 5

alert(x); // undefined

var x = 10;

alert(this.x); // 5

alert(x); // 10

}

foo();


但是,因为此运算符的值可以根据方式而改变a

函数被调用,可能需要替代。另一个选项是

来使用一些引用全局对象的变量。在大多数情况下,窗口

变量就足够了,虽然我更喜欢使用我自己的:


var global = this,

x = 5;


函数foo(){

alert(global.x);

alert(x);

var x = 10;

alert(global.x);

alert(x);

}

foo();


Mike

[1]< http://www.ecma-international.org/publications/standards /Ecma-262.htm>


-

迈克尔·温特

前缀主题[新闻]在回复e-之前邮件。


Over the weekend I attended a session on JavaScript at the No Fluff
Just Stuff conference and learned an interesting quirk that I wanted to
ask a question about..

Take this code:

x = 5;

function foo() {
alert(x);
var x = 10;
alert(x);
}

foo();

The first alert will display ''undefined'', and the second will display
10. Apparently JavaScript recognizes the ''var x = 10'' declaration
inside foo() even before that line is reached - it spots the
declaration, but not the initialization to 10. Only after x is set to
10 and the second alert is reached will the 10 be displayed.

My question is... is there any way from this code to access the global
x variable (5) from within foo()?

解决方案


Joe Attardi wrote:

Over the weekend I attended a session on JavaScript at the No Fluff
Just Stuff conference and learned an interesting quirk that I wanted to
ask a question about..

Take this code:

x = 5;

function foo() {
alert(x);
var x = 10;
alert(x);
}

foo();

The first alert will display ''undefined'', and the second will display
10. Apparently JavaScript recognizes the ''var x = 10'' declaration
inside foo() even before that line is reached - it spots the
declaration, but not the initialization to 10. Only after x is set to
10 and the second alert is reached will the 10 be displayed.

My question is... is there any way from this code to access the global
x variable (5) from within foo()?



Try the following:

alert(window.x);

or

alert(window["x"]);


Joe Attardi wrote:

Over the weekend I attended a session on JavaScript at the No Fluff
Just Stuff conference and learned an interesting quirk that I wanted to
ask a question about..

Take this code:

x = 5;

function foo() {
alert(x);
var x = 10;
alert(x);
}

foo();

The first alert will display ''undefined'', and the second will display
10. Apparently JavaScript recognizes the ''var x = 10'' declaration
inside foo() even before that line is reached - it spots the
declaration, but not the initialization to 10.
No - it returns that x is undefined. That is, ''x'' has not yet been
defined. You define it on the following line.
Only after x is set to
10 and the second alert is reached will the 10 be displayed.
Because now, x has been defined.
My question is... is there any way from this code to access the global
x variable (5) from within foo()?



Declare the global x variable as:
var x = 5;


On 15/03/2006 21:44, Joe Attardi wrote:

[snip]

x = 5;
All variables, including globals, should be explicitly declared using a
var statement.
function foo() {
alert(x);
var x = 10;
alert(x);
}

foo();

The first alert will display ''undefined'', and the second will display
10. Apparently JavaScript recognizes the ''var x = 10'' declaration
inside foo() even before that line is reached - it spots the
declaration, but not the initialization to 10. Only after x is set to
10 and the second alert is reached will the 10 be displayed.
Variable instantiation occurs before code execution begins, as described
in section 10.1.3 Variable Instantiation, ECMA-262 3rd Ed.[1]

Simply put, when the foo function is called, formal arguments, function
declarations, and variable declarations are processed to create local
variables. After that, execution begins with variable initialisation
occurring as assignments are evaluated.
My question is... is there any way from this code to access the global
x variable (5) from within foo()?



Yes, by access the global variable as a property of the global object.

With the code as-is, one can use the this operator to reference the
global object:

var x = 5;

function foo() {
alert(this.x); // 5
alert(x); // undefined
var x = 10;
alert(this.x); // 5
alert(x); // 10
}
foo();

However, as the value of the this operator can change depending on how a
function is called, an alternative may be necessary. Another option is
to use some variable that refers to the global object. The window
variable suffices in most cases, though I prefer to use my own:

var global = this,
x = 5;

function foo() {
alert(global.x);
alert(x);
var x = 10;
alert(global.x);
alert(x);
}
foo();

Mike
[1] <http://www.ecma-international.org/publications/standards/Ecma-262.htm>

--
Michael Winter
Prefix subject with [News] before replying by e-mail.


这篇关于奇怪的变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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