【发布时间】:2012-07-26 20:24:23
【问题描述】:
有了这个页面:
<!DOCTYPE html>
<html>
<head>
<script>
"use strict";
var foo = 2;
delete foo;
</script>
</head>
<body></body>
</html>
Firebug 控制台给出:
applying the 'delete' operator to an unqualified name is deprecated
>>> foo
ReferenceError: foo is not defined
foo
但是这样就成功了:
>>> var bar = 2;
undefined
>>> delete bar;
true
即使您注释掉delete foo; 以便脚本不会中断,删除bar 仍然是成功的,尽管它“是一个全局对象的属性,因为它是通过变量声明创建的,@987654321 也是如此@":
>>> foo
2
>>> delete foo
false
>>> var bar = 2;
undefined
>>> delete bar
true
是否可以在 FireBug 和/或 Chrome 的控制台中启用“严格模式”?
【问题讨论】:
-
我想知道控制台代码是否正在通过
eval()进行管道传输,在这种情况下不会设置 DontDelete 属性。 -
...好吧,如果我刚刚阅读了下一节:
And this is the gist of Firebug’s abnormal behavior. All the text in console seems to be parsed and executed as Eval code, not as a Global or Function one. -
我遵循了@zoranc 的第一个建议,这样我就可以看到严格模式在 chrome 的控制台中工作。
(function f() { 'use strict'; console.log('"this" here is: ', this, 'Strict Mode is cool...'); } ());(function f() { console.log('"this" here is: ', this, 'Global variables are evil! So Crockford told me...'); } ()); -
devtools 控制台应该有一个复选框来为所有命令启用严格模式
标签: javascript console strict-mode