I just found that in the last Chrome 42 and FF 37.0.2 this lines of code are perfectly legal
"use strict";
var o = { p: 1, p: 2 };
(copy-pasted from MDN )
In IE 10-11 and Opera 28.0.1750 it throws error as expected.
In the same time,
abc=0;
causes error (undeclared variable) as expected.
Does anybody know what caused such change?
The "use strict" Directive It is not a statement, but a literal expression, ignored by earlier versions of JavaScript. The purpose of "use strict" is to indicate that the code should be executed in "strict mode". With strict mode, you can not, for example, use undeclared variables.
what are the advantages and disadvantages to using it? If you put "use strict"; at the top of your code (or function), then the JS is evaluated in strict mode. Strict mode throws more errors and disables some features in an effort to make your code more robust, readable, and accurate.
Strict mode changes some previously-accepted mistakes into errors. JavaScript was designed to be easy for novice developers, and sometimes it gives operations which should be errors non-error semantics. Sometimes this fixes the immediate problem, but sometimes this creates worse problems in the future.
Using Strict mode for a function: Likewise, to invoke strict mode for a function, put the exact statement “use strict”; (or 'use strict';) in the function's body before any other statements. Examples of using Strict mode: Example: In normal JavaScript, mistyping a variable name creates a new global variable.
There is a Bugzilla ticket here. From what I gather (here and other pages I have looked up), duplicate properties are legal in ECMAScript version 6, opposed to ES5, where it is forbidden in strict mode.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With