最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

"use strict" in JavaScript doesn't work? - Stack Overflow

programmeradmin3浏览0评论

According to this article

Adding “use strict” as the first statement¹ in your JavaScript code will enforce Strict Mode over the entire

So why :

"use strict";
012; 

Doesn't throw errors

while

(function () {
    "use strict";
    012; })();

does ? (Octal literals are not allowed in strict mode.)

John resig says nothing about it. he just says :

Simple. Toss this at the top of a program to enable it for the whole script:

"use strict"; Or place it within a function to turn on strict mode only within that context.

function imStrict(){ "use strict"; // ... your code ... }

edit :

edit #2.

I tested the code in console.(chrome). in jsbin sample - it is working. still , I dont understand why it behave different in console.

According to this article

Adding “use strict” as the first statement¹ in your JavaScript code will enforce Strict Mode over the entire

So why :

"use strict";
012; 

Doesn't throw errors

while

(function () {
    "use strict";
    012; })();

does ? (Octal literals are not allowed in strict mode.)

John resig says nothing about it. he just says :

Simple. Toss this at the top of a program to enable it for the whole script:

"use strict"; Or place it within a function to turn on strict mode only within that context.

function imStrict(){ "use strict"; // ... your code ... }

edit :

edit #2.

I tested the code in console.(chrome). in jsbin sample - it is working. still , I dont understand why it behave different in console.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jan 20, 2013 at 14:49 Royi NamirRoyi Namir 149k144 gold badges492 silver badges831 bronze badges 7
  • What happens if you do "use strict"; 012; in one line? – melpomene Commented Jan 20, 2013 at 14:58
  • @melpomene same. just tested that. – Royi Namir Commented Jan 20, 2013 at 14:58
  • 1 Well, then whatever you're using as a REPL doesn't support top-level "use strict". – melpomene Commented Jan 20, 2013 at 14:59
  • 2 Don't test code in a console and expect identical behaviors. – the system Commented Jan 20, 2013 at 15:05
  • 1 A console is not a pure environment. Use any other strict mode example, and you'll see that it disables a global strict declaration, for example foo="bar" (no declaration). This is not the case for application code, where it actually counts. Additionally, there are usually methods define in the console that are not available in application code. Bottom line... if you see odd behavior in the console, first verify in an actual application. – the system Commented Jan 20, 2013 at 15:24
 |  Show 2 more ments

3 Answers 3

Reset to default 2

It does throw an error.

quentin@workstation:~ # cat > tmp/foo.js
"use strict";
012; 

quentin@workstation:~ # node tmp/foo.js

/users/quentin/tmp/foo.js:2
012; 
^^^

module.js:434
  var piledWrapper = runInThisContext(wrapper, filename, true);
                        ^
SyntaxError: Octal literals are not allowed in strict mode.
    at Module._pile (module.js:434:25)
    at Object..js (module.js:464:10)
    at Module.load (module.js:353:31)
    at Function._load (module.js:311:12)
    at Array.0 (module.js:484:10)
    at EventEmitter._tickCallback (node.js:190:38)

The console doesn't behave the same way as other places, try opening the following in your browser and you'll see the error re-appear without the need to wrap it in a function.

<!doctype html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>use strict</title>
        <script>
"use strict";
012;
        </script>
    </head>
    <body>
    </body>
</html>

You can tell that the console is implemented differently to direct execution by typing just ~, +, etc (you'll get a SyntaxError: Unexpected token }).
It is possible to reproduce similar behaviour by writing code like this directly (I won't call it the same because I don't know how the console is doing it)

example: { // labeled block
    "use strict"; // your code
    012;
} // end of block, no SyntaxError thrown for strict

012 is an octal literal which is not allowed in strict mode as it is deprecated by edition 3 of ECMA-262. JavaScript 1.5 still supports that octal integer literal for backwards patibility. and your example IS throwing an error!

you have 3 possibilities.

  1. remove that octal literal

  2. dont use the strict mode

  3. or you can use the octal literal first, and wrap all your code in an anonymous function and restrict the strict mode to your immediately invoked Function Expression (iife):

012;

(function () {
  "use strict";

  ... your code
})();
发布评论

评论列表(0)

  1. 暂无评论