- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这本书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 规范的部分内容,我们将不胜感激。
谢谢。
<小时/>好的,我看到的结果是这样的,因为我已经在 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 的变量会导致引用错误我不确定我能否理解其余的输出,特别是为什么 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):
“onLoad”选择器意味着您在界面的 JavaScript 象限中键入的代码将被包装在一个函数中,并且该函数将充当窗口的“load”事件处理程序。因为您的代码位于函数中,所以您在看似全局级别声明的变量实际上不是全局的;它们是函数中的局部变量。
要使您的代码真正全局化,请将该选择更改为“不换行”设置之一。
关于javascript - “PHP 开发人员的 JavaScript -> 内置 API -> 全局对象”: cannot reproduce code in browser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26675008/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!