2

Reading about ECMAScript 5's strict mode I learn that:

Certain language functions are so pervasive that performing runtime checks has considerable performance cost. A few strict mode tweaks, plus requiring that user-submitted JavaScript be strict mode code and that it be invoked in a certain manner, substantially reduce the need for those runtime checks.

Some of those tweaks could be:

  • Never use null/undefined as first argument in a call/apply method.
  • Do not create new globals variables inside a function.

Questions:

  1. Are there some JavaScript frameworks written in strict mode? So we can trust that our "strict" code will not be wasted?

  2. Should I start paying attention to strict mode no matter what? I mean is it blindly adopted as a good practice?

Gergely Fehérvári
  • 7,811
  • 6
  • 47
  • 74
Igor Parra
  • 10,214
  • 10
  • 69
  • 101
  • 1
    Your second question is an opinion question, but I think most people would think that Strict mode is generally a really useful tool and it prevents a lot of bugs. – Pointy Dec 11 '11 at 18:52
  • @pointy I refer if it is really convenient or maybe a waste of time. – Igor Parra Dec 11 '11 at 19:06
  • 2
    *"So we can trust that our "strict" code will not be wasted?"* - What does that mean? Why would your code be wasted? Strict mode is the future of JavaScript. It is an improvement. I don't see why you wouldn't want to use it in new projects... – Šime Vidas Dec 11 '11 at 19:14
  • 2
    BTW, why "never use `null`/`undefined` as the first argument to `apply`/`call`"? You _can_ use them, they just don't get automatically transformed into global object... –  Dec 11 '11 at 19:19
  • @ime-vidas I say it because all code must be written in `strict mode` otherwise the "non-strict" parts will thrown exceptions. – Igor Parra Dec 11 '11 at 19:20
  • @herby that's the idea `strict mode` will not tolerate this kind of automatic assumptions. See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call#Parameters – Igor Parra Dec 11 '11 at 19:23
  • @NomikOS For new scripts (projects), use strict mode. For existing code, check if adding strict mode throws. If yes, consider updating the code so that it doesn't throw (if you have the time/will). – Šime Vidas Dec 11 '11 at 19:26
  • @herby: +1, I even use that especially because I mean it. – Bergi Dec 11 '11 at 19:30

2 Answers2

3
  1. Yes, most of the frameworks already started to use strict mode. Maybe not complete yet but in the close distance. John Resig:

    All that being said, I'm fairly certain that jQuery is ES5-Strict compatible right now. Once an implementation of the language is made available (so that that premise may be tested) I'll happily switch jQuery over to working exclusively in strict mode.

  2. It is recommended to use, but not required. If you start to use strict mode now, you will get used to it and therefore you will be ready for the new standard.

More aboute strict mode:

Gergely Fehérvári
  • 7,811
  • 6
  • 47
  • 74
2

AFAIK jQuery is written in strict mode (EDIT: not so de iure, but author says it is compatible). Crockford's jslint uses "use strict" at the very beginning (I saw the source code few days ago). I'd say nearly every modern framework which can afford that is strict mode, since it is a bonus point for the framework itself.

Even it wasn't so, your work is not lost, since by adhering to strict mode, you make your code better (it forces you to avoid some of the sins).

  • Ok, maybe it was only John Resig in some blog saying jQuery is strict mode-compatible or something like that. –  Dec 11 '11 at 19:04
  • @Domenic not the `"use strict"` is the strict mode. It only activates the mode. I think the code is ready to used in strict mode and if implementation allow it, we will see the `"use strict"` statement in jQuery as well. – Gergely Fehérvári Dec 11 '11 at 19:08
  • I think that jquery can be put in `strict mode` but was not really written from the beginning with that in mind: http://forum.jquery.com/topic/using-jquery-in-strict-mode-generates-a-lot-of-warnings – Igor Parra Dec 11 '11 at 19:08
  • At the time jQuery started there was no (definite definition of) strict mode in the first place... :-) –  Dec 11 '11 at 19:12