' PHP for PHP Developers - >内置API - >全局对象':无法在浏览器中重现代码

时间:2014-10-31 12:42:41

标签: javascript variables global-variables scope global

本书JavaScript for PHP Developers包含以下注释代码(to 我添加了alert()来显示单变量表达式语句的值 形式

variable;

我还添加了'use strict'指令,看看是不是这样 造成这个问题。我无法使用Firefox重现code on JSFiddle。 我已将自己的评论添加到大型大写的代码中:

'use strict';

// Create a global variable
var john = "Jo";
alert(john);        // "Jo"
alert(window.john); // "Jo", works as a property too
                    /* BUT I GET UNDEFINED HERE */
// Create a property of the global object
window.jane = "JJ";
alert(jane);        // "JJ", works as a variable too
alert(window.jane); // "JJ"

// Delete them
alert(delete window.john); // false
                           /* BUT I GET true HERE */
alert(delete window.jane); // true

alert(john); // "Jo"
alert(jane); // undefined
             /* BUT PROGRAM CRASHES HERE */
alert(this === window); // true

事实上,在following small program最后一次警报函数调用永远不会到达:

window.jane = "JJ";
delete window.jane;
alert(jane); // Program Crashes
alert('Got Here');

我已经在这里再次测试了所有案例,这说明了所有案例。

var a = 'John';
window.b = 'Jane';
c = 'Jack';

alert(a); // John
alert(b); // Jane
alert(c); // Jack

alert(window.a); // undefined
alert(window.b); // Jane
alert(window.c); // Jack

alert(delete a); // false
alert(delete b); // true
alert(delete c); // true

alert(a); // John
//alert(b); // would crash
//alert(c); // would crash

window.b = 'Jane';
c = 'Jack';

alert(delete window.a); // true
alert(delete window.b); // true
alert(delete window.c); // true

alert(window.a); // undefined
alert(window.b); // undefined
alert(window.c); // undefined

alert(a); // John
//alert(b); // would crash
//alert(c); // would crash

我想知道的是,这种行为在所有浏览器中都是一致的 或者是一个浏览器与另一个浏是代码来自 预订错误,或者只是针对不同的浏览器运行 我自己的(Firefox 33.0.1)?

如果有人可以解释各种情况,或许指向相关的 ECMA规范的各个部分,将不胜感激。

感谢。


好的,我看到的结果是这样的,因为我在JSFiddle中运行了代码 正如所指出的那样,onload函数而不是全局范围。这是结果 在我的本地计算机上提供的网页中运行综合测试。 JSFiddle with the script inside the body html element上的结果是相同的:

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var a = 'John';
window.b = 'Jane';
c = 'Jack';

alert(a); // John
alert(b); // Jane
alert(c); // Jack

alert(window.a); // John
alert(window.b); // Jane
alert(window.c); // Jack

alert(delete a); // false
alert(delete b); // true
alert(delete c); // true

alert(a); // John
try { alert(b); } catch (e) { alert(e); } // throws ReferenceError: b is not defined
try { alert(c); } catch (e) { alert(e); } // throws ReferenceError: c is not defined

window.b = 'Jane';
c = 'Jack';

alert(delete window.a); // false
alert(delete window.b); // true
alert(delete window.c); // true

alert(window.a); // John
alert(window.b); // undefined
alert(window.c); // undefined

alert(a); // John
try { alert(b); } catch (e) { alert(e); } // throws ReferenceError: b is not defined
try { alert(c); } catch (e) { alert(e); } // throws ReferenceError: c is not defined
</script>
</body>
</html>

here is what happens when the ECMAScript5 'use strict' directive is also used。而 我知道在严格模式下声明一个没有var的变量会导致引用错误 我不确定我能否理解输出的其余部分,特别是为什么脚本 执行在某些地方终止:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
'use strict';

var a = 'John';
window.b = 'Jane';
try { c = 'Jack'; } catch (e) { alert(e); } // throws ReferenceError: assignment to undeclared variable c

alert(a); // John
alert(b); // Jane
try { alert(c); } catch (e) { alert(e); } // throws ReferenceError: c is not defined

alert(window.a); // John
alert(window.b); // Jane
alert(window.c); // undefined

try {
// Uncommenting any of these three following statements will cause the script to be exited
// during the parsing time; no statement from this script will be executed.
//alert(delete a);  causes script to end during parsing at runtime even though try catch block present
//alert(delete b);  causes script to end during parsing at runtime even though try catch block present
//alert(delete c);  causes script to end during parsing at runtime even though try catch block present
} catch (e) { alert(e); }

alert(a); // John
try { alert(b); } catch (e) { alert(e); } // Jane
try { alert(c); } catch (e) { alert(e); } // throws ReferenceError: c is not defined

window.b = 'Jane';
c = 'Jack';

try {
//alert(delete window.a); // causes script to end during execution at runtime even though try catch block present
//alert(delete window.b); // causes script to end during execution at runtime even though try catch block present
//alert(delete window.c); // causes script to end during execution at runtime even though try catch block present
} catch (e) { alert(e); }

/* Script stops execution at this point. Why?????

alert(window.a); // 
alert(window.b); // 
alert(window.c); // 

alert(a); // 
try { alert(b); } catch (e) { alert(e); } // 
try { alert(c); } catch (e) { alert(e); } // 
</script>
</body>
</html>

如果有人可以帮我解释脚本执行在某些地方终止的原因 我们非常感谢他们采用严格模式的方式。

感谢。


关于本书中的origninal代码,从脚本中正确运行时 在文档的头部标记,我得到以下输出,我们可以看到一个 由于访问变量jane而引发的ReferenceError实例被抛出。 Here is the JSFiddle for the code

//'use strict'

// Create a global variable
var john = "Jo";
alert(john);        // "Jo"
alert(window.john); // "Jo", works as a property too

// Create a property of the global object
window.jane = "JJ";
alert(jane);        // "JJ", works as a variable too
alert(window.jane); // "JJ"

// Delete them
try { alert(delete window.john); } catch (e) { alert(e); }
// false
/* if strict mode were enforced would actually cause
   the following fatal error:
   TypeError:
   property "john" is non-configurable and can't be deleted   
*/
try { alert(delete window.jane); } catch (e) { alert(e); }
// true

alert(john); // "Jo"
try { alert(jane); } catch (e) { alert(e); } // undefined in book
/* but actually gives a:
   ReferenceError: jane is not defined
   which is a fatal error causing the script to exit
   if not caught*/
alert(this === window); // true

1 个答案:

答案 0 :(得分:1)

查看jsfiddle界面的这一部分:

jsfiddle screenshot

“onLoad”选择器意味着您在界面的JavaScript象限中键入的代码将被包装在一个函数中,并且该函数将充当窗口的“加载”事件处理程序。因为你的代码在一个函数中,你在出现所声明的变量是全局级别的,实际上并不是全局的;它们是函数中的局部变量。

要使您的代码真正全局化,请将该选择更改为“无包装”设置之一。