Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why using `strict mode` in JavaScript libraries? [duplicate]

Tags:

Possible Duplicate:
What does “use strict” do in JavaScript, and what is the reasoning behind it?

Actually I know what the use strict does in JavaScript as the question asked here:
What does "use strict" do in JavaScript, and what is the reasoning behind it?

But I can't understand why we should use strict mode in JavaScript libraries? I mean what's the benefits of using that?

like image 401
Afshin Mehrabani Avatar asked Nov 21 '12 05:11

Afshin Mehrabani


People also ask

Why do we use strict mode in JavaScript?

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.

What are the advantages and disadvantages of using use strict?

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.

What is the benefit of using use strict `?

First, strict mode eliminates some JavaScript silent errors by changing them to throw errors. Second, strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode.

What happens in strict mode JavaScript?

Strict mode prohibits some syntax likely to be defined in future versions of ECMAScript. It prevents, or throws errors, when relatively “unsafe” actions are taken (such as gaining access to the global object). It disables features that are confusing or poorly thought out.


1 Answers

The question you linked, its answers, and the references it gives list a bunch of reasons for using strict mode.

Let me call out just one of them: The Horror of Implicit Globals¹

Non-strict code:

function foo(a) {     var myvar;      myar = a * 4;      // ...      return myvar; } 

Now, this code:

console.log(foo(2)); 

...should log "8", right? But it doesn't, it always logs "undefined":

function foo(a) {      var myvar;        myar = a * 4;        // ...        return myvar;  }  console.log(foo(2));

And what's more, it silently creates a global variable called myar. Why? Because I had a typo in my code (I missed out the v in myvar when setting it to a * 4).

Compare with:

function foo(a) {      "use strict";      var myvar;        myar = a * 4;        // ...        return myvar;  }  console.log(foo(2));

Now, instead of a weird return value and a global variable getting created, I get a nice error message: ReferenceError: "myar" is not defined

Now, that particular aspect of strict mode could be accomplished using a lint tool instead. But you don't always involve a lint tool in your coding-in-anger toolchain, when you're just trying to fix something and bouncing between your editor and the browser. So it's nice when the browser helps you out.

Separately, strict mode does things that can't be done by lint tools, such as disallowing with, changing the default value of this in function calls that don't set it, etc.


¹ (that's a post on my anemic little blog)

like image 167
T.J. Crowder Avatar answered Nov 10 '22 01:11

T.J. Crowder