可能启用“严格模式”;在FireBug和Chrome的控制台? [英] Possible to enable "strict mode"; in FireBug and Chrome's console?
问题描述
使用此页面:
<!DOCTYPE html>
<html>
<head>
<script>
"use strict";
var foo = 2;
delete foo;
</script>
</head>
<body></body>
</html>
Firebug控制台提供:
Firebug console gives:
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; code>,以便脚本不会中断,删除
bar
仍然成功,尽管事实它是一个全局对象的属性,因为它是通过变量声明创建的 DontDelete属性:
Even if you comment out delete foo;
so that the script does not break, deleting bar
is still successful despite the fact it "is a property of a Global object as it is created via variable declaration and so has DontDelete attribute":
>>> foo
2
>>> delete foo
false
>>> var bar = 2;
undefined
>>> delete bar
true
可以启用strict mode在FireBug和Chrome的控制台中?
Is it possible to enable "strict mode"; in FireBug and or Chrome's console?
推荐答案
firebug控制台通过在eval调用中包装所有代码,语句在你的脚本中不再是使用严格 - 因此它被禁用。你可以尝试在一个函数中包装你的代码,以强制执行使用严格的特定功能,但我知道的最好的解决方案是跳过控制台,并直接在页面本身测试。
The firebug console works by wrapping all the code in an "eval" call so the first statement in your script is no longer "use strict" - hence it is disabled. You could try wrapping your code in a function to enforce "use strict" for that particular function but the best solution I know of is to skip the console and test straight in the page itself.
这篇关于可能启用“严格模式”;在FireBug和Chrome的控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!