Why using "use strict" can save you time

Source: Internet
Author: User

Ext.: http://ourjs.com/detail/52f572bf4534c0d806000024

"Use strict" is a very good feature in JavaScript and is very easy to work with.

How to use

File.js
"Use Strict"
function Dostuff () {
Use Strict is enabled here!
}

Such Tiao file.js will be applied to the "use strict" mode.
If you want to use only one function:

File.js
function A () {
"Use strict";
Use strict are enabled in the this context
function Nestedfunction () {
And here too
}
}
Benefits

Check for duplicate keys in an object

var zombie = {
eyeleft:0,
Eyeright:1,
... a lot of keys ...
Eyeleft:1
}


This code throws an error because Eyeleft appears two times. It's much quicker than looking for a mistake with your eyes.

Variable not declared

plane = 5;

You now know that you forgot to add var before this variable. However, if you do not know, debugging is very painful, because this variable is declared in the global context, and may be removed elsewhere. Imagine that if you declare a global I, it can cause confusion in nested loops.

Duplicate parameters

function Run (Fromwhom, Fromwhom) {}

Note that fromwho appears two times and therefore throws an error.

Arguments in a restricted function

var run = function (Fromwhom) {
Arguments[0] = ' alien ';
alert (Fromwhom);
}
Run (' zombie ');
Alert: ' Alien ';

Now you can use the "using strict"

var run = function (Fromwhom) {
"Use strict";
Arguments[0] = ' alien ';
alert (Fromwhom);
}
Run (' zombie ');
Alert: ' Zombie ';

Arguments[0] = ' alien ' changed the parameters Fromwhom,use strict and saved your time.



Original address: webdesignporto.com

Why using "use strict" can save you time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.