admin管理员组文章数量:1384512
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
3 Answers
Reset to default 2It 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.
remove that octal literal
dont use the strict mode
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
})();
本文标签: quotuse strictquot in JavaScript doesn39t workStack Overflow
版权声明:本文标题:"use strict" in JavaScript doesn't work? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744521976a2610509.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论