Writing specifications for JavaScript

Source: Internet
Author: User

Write maintainable code

Maintenance, modification, and extension of code are both time and labor-intensive, so in order to reduce unnecessary costs, it is necessary to write maintainable code from the outset, so that you can also facilitate the project to others.

Writing maintainable code means that your code is:

    1. Readable
    2. Consistency
    3. Predictable
    4. Maintainable and extensible
Global variables

Definition of global variables:

1.var + variable names are declared outside of function. is a global variable.

<script>var global = ' Test ', function Getglobalvalue () {    console.log (Global)}getglobalvalue ()//' Test ' </ Script>

2. Declare global variables implicitly without using Var

<script>global = ' test '; function Getglobalvalue () {    global_1 = ' test1 ';    Console.log (global);} Getglobalvalue (); ' Test ' console.log (global_1); ' Test1 ' </script>

3. Use window Global to declare

<script>window.global = ' test '; Console.log (global) </script>

You should try to avoid using global variables. Excessive use of global variables can cause problems:

1. Global variables are stored in the static storage area. With the dynamic allocation of local variables, the dynamic release of memory compared to a longer lifetime, will occupy more memory units.

2. Global variables undermine the encapsulation performance of a function.

3. Global variables make the code readability of the function less readable. Because multiple functions may use global variables, the values of global variables may change at any time when the function is executed, which is detrimental to the troubleshooting and debugging of the program.

For-in cycle (for-in Loops)

For-in loops are primarily used for traversal of non-array objects. It also iterates through the prototype chain, using hasOwnProperty() methods that can filter out the methods from the prototype chain.

var student = {name: ' Xiaoming ', age:18,sex: ' Man '}//add a method to all objects if (typeof Object.prototype.clone = = = "undefined") {
   object.prototype.clone = function () {};} Traversing the object properties, there is no filtering prototype method for the (var item in student) {Console.log (item + ': ' + Student[item])}/*result:name:xiaomingage:18sex: Manclone:function () {}*///uses hasOwnProperty () to filter the prototype method for (Var item in student) {if (Student.hasownproperty (item)) { Console.log (item + ': ' + Student[item])}}//another way var item, Hasown = Object.prototype.hasownproperty;for (item in student) { if (Hasown.call (student, item)) {Console.log (item + ': ' + Student[item])}}/*result:name:xiaomingage:18sex:man*/
Other specifications

Other specifications include spaces, indents, circular curly braces, function naming, and annotations, as well as uniform specifications. Easy code reading and multi-person collaboration.

Resources

1. Uncle Tom's in-depth understanding of the JavaScript series

Writing specifications for JavaScript

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.