可能启用“严格模式".在FireBug和Chrome的控制台中?

问题描述:

使用此页面:

<!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;以便脚本不会中断,删除bar仍然是成功的,尽管事实它是Global对象的属性,因为它是通过变量声明创建的,因此 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

是否可以在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.