Tuesday, September 29, 2015

JavaScript: Do We Have to Put "use strict" Every Time?

Well, no.

This new directive feature introduced in ECMAScript 5 is supported on many latest browsers. For Internet Explorer, this is supported on IE 10 and above (Edge).

This feature is intended for error checking and security (and to avoid collisions).
It's very handy if we put it at the beginning of a function block. But it will be a whole different topic as a global directive.
As in:

In the example above, the var "number" is declared within the "test" function, not as global variable.
But if we re-type that like this:

Firefox browser always implements this feature nicely.
But on latest Chrome you'll see "nothing". Chrome is pretty "cool" for this feature.


Put it as the beginning of a function block

Or, anonymous auto-invoked:

Declaring the directive as a global directive, i.e. on top of everything, will create a different behavior. In the example above, we just wanna secure our own application (the function) and let the variables outside our custom function(s) non-strict.


JSLint

JSLint always encourages JavaScript programmers to make use of this feature. But of course, for, like, a super dooper my-own-application-style flow will make you wanna punch something. Especially when the application you've built depends on another framework. Because it will take A LOT of re-tinkering.

But in our experiences, re-tinkering application (in any programming language, front/back-end, online/offline) is always needed to gain high(er) performance and efficiency (and secure it even further) so that you'll break "the boundaries". And will spark "lots of ideas" for the new features in that particular application or maybe a brand new one.


How about olden browsers?

As long as we keep the flow "normalized", not so much (untested) prototype this, prototype that, it will still run smoothly.
More than 50% of chance.

Unless there's time travel machine we can buy on market, we can, like, buy it.


Links

  1. What is ECMA? On ECMA.
  2. John Resig's post about "use strict".
  3. Strict mode documentation on MDN.
  4. Strict mode documentation on MSDN.
  5. Strict mode support table on CanIUse.
  6. The JSLint.

No comments:

Post a Comment

Tell me what you think...