10 best javascript development practices you need to know

Source: Internet
Author: User
Many of the Extended features of Javascript are that it is becoming more sharp, and it also gives programmers the opportunity to create more beautiful and more user-friendly websites. Although many developers are happy to celebrate javascript, but some people still see its dark side.

Web pages using a lot of javascript code are slow to load. using javascript too much makes webpages ugly and slow. Soon how to use javascript effectively becomes a hot topic.

Here let's list 10 best javascript practices to help you effectively use javascript.

1. Keep the code as concise as possible
You may have heard about this concise code N times. As a developer, you may have used it many times in your code development process, but never forget it in js development.

Try to add comments and spaces in the Development Mode to keep the code readable.
Before publishing to the product environment, delete spaces and comments, and try to abbreviations variables and method names.
Use third-party tools to compress javascript.
2. After thinking about it, modify prototypes.
Adding new properties to the prototype object is a common cause of script errors.

The Code is as follows:


YourObject. prototype. anotherFunction = 'hello ';
YourObject. prototype. anotherMethod = function (){... };


In the above Code, all variables will be affected because they all inherit from "yourObject ". Such use will lead to unexpected behavior. Therefore, it is recommended to delete similar modifications after use.

The Code is as follows:


YourObject. prototype. anotherFunction = 'hello ';
YourObject. prototype. anotherMethod = function (){... };
Test. anotherMethod ();
Delete yourObject. prototype. anotherFunction = 'hello ';
Delete yourObject. prototype. anotherMethod = function (){... };


3. Debug Javascript code
Even the best developers make mistakes. To minimize similar errors, run your code in your debugger to confirm that you have not encountered any minor errors.

4. Avoid Eval
Your JS can work well without the "eval" method. "Eval" allows access to the javascript compiler. If a string is passed to "eval" as a parameter, its result can be executed.

This greatly reduces the code performance. Avoid using "eval" in the product environment whenever possible ".

5. Minimize DOM access
DOM is the most complex API that slows down code execution. Sometimes the web page may not be loaded or is incomplete. It is best to avoid DOM.

6. Learn javascript before using the javascript class library
The Internet is flooded with many javascript class libraries. Many programmers often use js class libraries without understanding the negative effects. It is strongly recommended that you learn the basic JS Code before using a third-party class library. Otherwise, you will be prepared for bad luck.

7. Do not use the "SetTimeOut" and "Setinterval" methods as alternatives for "Eval ".
SetTimeOut ("document. getID ('value')", 3000 );
In the above Code, document. getID ('value') is processed as a string in the "setTimeOut" method. This is similar to the 'eval' method. Executing a string in each code execution reduces performance. Therefore, it is recommended to pass a method in these methods.

SetTimeOut (yourFunction, 3000 );
8. [] is better than "new Array ();"
A common mistake is to use an array when an array is needed or when an object is used. However, the usage principle is simple:

"When the attribute name is a small continuous integer, you should use an array. Otherwise, use an object "-Douglas Crockford, author of JavaScript: Good Parts.

Suggestion:
Var a = ['1a ', '2b'];
Avoid:

Var a = new Array ();
A [0] = "1A ";
A [1] = "2B ";
9. Try not to use var multiple times
At the beginning of every variable, programmers are used to using the "var" keyword. Instead, we recommend that you use commas (,) to avoid unnecessary keywords and reduce the code size. As follows:

Var variableOne = 'string 1 ',
VariableTwo = 'string 2 ',
VariableThree = 'string 3 ';
10. Do not ignore the Semicolon ";"
This is often one of the reasons why everyone spent several hours debugging.

I'm sure you have read the above content in other articles, but you may often ignore many basic rules. Have you ever ignored the excessive number. Have you also encountered performance problems caused by eval keywords? Thank you!
Related Article

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.