标签云

微信群

扫码加入我们

WeChat QR Code


Changing the default after so many years ? Too late for that : it would break so many existing sites/scripts/applications... The only possible thing is to help make things better, for the future.

2018年10月22日50分39秒

I tried a small code snippet that would be invalid when using "use strict" in Firefox 3.6, Safari 5, Chrome 7 and Opera 10.6 (all Mac). No errors whatsoever, so i guess 'use strict' is not supported in any browser yet. Didn't test in IE9 though ;)

2018年10月22日50分39秒

Quick update: Firefox 4 has complete support for strict mode, and as far as I can tell, no other browser does. Safari and Chrome have "partial" support, but I don't really know what that means.

2018年10月22日50分39秒

Chrome 11 seems to pass all of these tests as does IE10 ie.microsoft.com/testdrive/HTML5/TryStrict/Default.html#

2018年10月22日50分39秒

Julius - This couldn't have been implemented using a reserved keyword, because then code trying to trigger strict mode would break in old browsers. Adding a "random" string literal doesn't break anything.

2018年10月22日50分39秒

Fail fast and fail loudly.

2018年10月22日50分39秒

If you are writing Javascript inline in HTML files, start each new block with <script>"use strict";. The flag only applies to the block in which it is included.

2018年10月23日50分39秒

It's funny, this resulted in strings must have single quotes. So write 'use strict'; instead

2018年10月22日50分39秒

then what would happen to the hoisting concept of javascript ?

2018年10月22日50分39秒

When I'm adding "use strict"; to the top of my JavaScript code, I am asked to use the function form of 'use strict' instead. Can you explain why is better to use the function form of 'use strict'? Thank you

2018年10月22日50分39秒

Nit: global variables are allowed, just have to be explicit (e.g. window.foo = bar).

2018年10月22日50分39秒

Requires all property names in an object literal to be unique (var x = {x1: "1", x1: "2"}) is this valid

2018年10月22日50分39秒

Your example in 11 is missing a modification of a (otherwise it makes no sense). I. e. function sum(a,b) { a = 0; return arguments[0] + b; } alert(sum(1, 2)) will return 3 with strict mode and 2 without strict mode, due to aliasing.

2018年10月22日50分39秒

I disagree. I think this shows why its very useful. In essence it means that this returns its function and not the window

2018年10月22日50分39秒

when do you ever want the window with this that you can't target with window?

2018年10月22日50分39秒

It refers to itself. this belongs to its own function and not the global window

2018年10月22日50分39秒

In the second one this one is actually undefined.

2018年10月22日50分39秒

The point is that your JS program will start failing due to accessing a property of an undefined, instead of silently doing the wrong thing on the global object. Makes tracking down subtle bugs much easier.

2018年10月22日50分39秒

WHY are devs in this thread so cavalier about "use strict"?? It THROWS EXCEPTIONS in otherwise working JavaScript, for goodness sakes! Just sprinkle it on the code like sugar on Corn Flakes, eh? NO! BAD! "use strict" should be used cautiously, preferably only in code you control that has unit tests that pass against all major browsers and that exercise all code paths. You got tests? Okay, "use strict" is fine for you, knock yourselves out.

2018年10月22日50分39秒

Yes. Obviously "use strict" can break seemingly valid javascript which hasn't broken before. But the code not having broken before is not equal to the code being correct and doing what it's supposed to. Usually referencing undeclared variables signals a typo, etc. Use strict allows you to catch these kinds of errors, and hopefully before you ship production code.

2018年10月22日50分39秒

... or just apply "use strict" as part of a last pass over your code, fix all the obvious problems, shrug, say "good enough," then take it out for production :)

2018年10月22日50分39秒

Personally, I never/very rarely add "use strict"; to existing code. That being said, I'll almost always use it when I'm writing new code from scratch

2018年10月22日50分39秒

If you're already using JSLint, you've probably fixed most of the places where "use strict" would break things, though.

2018年10月23日50分39秒

it is very nice explanation. However, I have one doubt that can I use "strict" mode in conjunction with other java script libraries, like Angular js?

2018年10月22日50分39秒

UVM: The strict-mode directive affects only lexical scope. i.e. only the file/function that it is declared. If you have another file/function that does not have the 'use strict' directive, they will be executed in non-strict mode, even when called from a function running in strict mode. See this asnwer for an explanation.

2018年10月22日50分39秒

thanks for your explanation. I understand that "strict" mode affects only the "code" , not its "execution"

2018年10月22日50分39秒

On second look, you're right. I thought you meant it only threw exceptions, but didn't change the way the code worked (like changing this). Now I see you were referring to calling other functions.

2018年10月22日50分39秒

There are some cases where octal is useful. The C syntax for it is horrible, but I would have liked to have seen languages add a new octal syntax which could then allow the leading-zero form to be deprecated. Of course, for Javascript to have supported the leading-zero form was just silly.

2018年10月22日50分39秒

JamieHutber : Please visit this link caniuse.com/use-strict AND kangax.github.io/es5-compat-table. It will give exact idea for all browser.

2018年10月22日50分39秒

"In strict mode octal literals are not allowed (these are literals that start with 0x ...)" octal literals start with a leading 0.

2018年10月22日50分39秒

Then what does it do?

2018年10月22日50分39秒

... this describes in part the compatibility, but not what it actually does.

2018年10月22日50分39秒

The reason strict mode is optional has nothing to do with what you've stated. The real reason is to not break existing code that may not conform.

2018年10月22日50分39秒

Indeed, the less experienced coders ought to be the first ones to enable the "use strict";

2018年10月22日50分39秒

Notice that the code above will add the i variable to the global scope (generally this not a best practice and strict mode helps to avoid that).

2018年10月22日50分39秒