gpt4 book ai didi

javascript - “PHP 开发人员的 JavaScript -> 内置 API -> 全局对象”: cannot reproduce code in browser

转载 作者:行者123 更新时间:2023-12-03 11:32:34 27 4
gpt4 key购买 nike

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

variable;

并且我还添加了“use strict”指令来查看是否是这样导致问题。我无法重现 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 规范的部分内容,我们将不胜感激。

谢谢。

<小时/>

好的,我看到的结果是这样的,因为我已经在 J​​SFiddle 中运行了代码正如所指出的, 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 的变量会导致引用错误我不确定我能否理解其余的输出,特别是为什么 script执行在某些地方终止:

<!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>

如果有人可以帮助我解释为什么脚本执行在某些地方终止他们使用严格模式的方式将不胜感激。

谢谢。

<小时/>

关于书中的原始代码,当从脚本中正确运行时标签在文档的头部,我得到以下输出,我们可以看到由于访问变量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

最佳答案

看看jsfiddle的这部分接口(interface):

jsfiddle screenshot

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

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

关于javascript - “PHP 开发人员的 JavaScript -> 内置 API -> 全局对象”: cannot reproduce code in browser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26675008/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com