Maintenance of high javascript

 

Develop good coding habits, improve code maintainability

  • Avoid define global variables or functions

Define global variables and functions, it will affect the maintainability of the code. If the javascript code running in the page in the same scope inside, then there is a possibility of mutual influence between the code might.

There are a variety of means to solve the problem because the definition of global variables lead to code "pollution".

1. The easiest way is to variables and methods packaged on a variable object, so that it becomes the object's properties. E.g:

var myTes = {

length:0'

init:function(){...},

action:function(){...}

}

2 is another modified embodiment of the global variables contained in a local scope, completion logic then these variables and variables used in the definition of the scope. For example, can be achieved by defining an anonymous function:

(function()

{

var length=0;

function init(){...}

function action(){...}

})()

All are included in the logic of this anonymous function performed immediately, forming an independent module, the maximum prevent "pollution" between code. Of course, in the actual business, there is interaction between the modules, then use the return statement, return interface requires disclosed.

var myDocument=(function()

{

var length=0;

function init(){...}

function action(){...}

return {

init:init

}

})();

 

When the external code access init () method, you can call the myDocument.init. This code is done cleverly code logic package, and requires access to an external interface disclosed is one modular best practice.

3. Make sure that the var keyword when defining variables. If you do not use var when defining variables, when the browser parses and does not complain, but this variable automatically resolves to global variables, such as the following code defines a global variable length:

(function()

{

length=0;

function init(){...}

function action(){...}

}

)()

  • Simplified encoding

// object creation

var persion=new Object();

person.age=23;

person.name='daniel';

Read:

var person={age:23,name:'daniel'};

// Array creation

var list=new Array();

list[0]=12,

list[1]=20;

list[2]=24;

Read:

was list = [12,20,24];

  • Instead of using a comparison operator == ===

  • Avoid using the with statement

In javascript, with statement can be used to quickly and easily access the object properties. statement with the following format:

with(object)

{

.......

}

with the statement principle is: when javascirpt parsing and running, will establish a scope with statements alone, and in combination with statements of property has become a local variable for this scope, it can be accessed directly. E.g:

with(Math)

{

a=PI*r*r;

}

 The following code is equivalent to the above code

a=Math.PI*r*r;

Use with statement does simplify the code, but with statement may also have brought some bug compatibility problems

  1. It makes the code difficult to read, for internal variables with statement quoted only at run-time variables in order to know which object belongs. E.g:

           function f(x,o)

       {

            with(o)

            {

                  print(x);

            }

       }

           The above statement with x, may be derived from parameters x, x property may come from the o parameter.

        Design 2.with statement is also flawed, after the object with the statement and with statements combine internal modifications and can not be synchronized with the internal, that is no guarantee that the consistency of object data. E.g:

          were group = {

          value:{

      node:1

            }

         };

         with(group.value) 

   {

     group.value={

      node:2

      };

     // error: 1

     console.log(node);

   }

         // display correctly: 2

        console.log(group.value.node);

  •      Avoid using eval

  1.      Sometimes using the eval function makes the code difficult to read, it affects the maintainability of the code. Such as:

                If the attribute value of an object acquired by a variable, you can use eval ( 'obj.' + Key);

          In fact, property values ​​can be obtained using the following standard method: obj [key]

        2. eval also use security problems, because it would execute arbitrary code passed, and the incoming code is likely to come from an unknown source or uncontrolled.

 

 

The use of more stringent encoding format

       The purpose of the strict mode is primarily intended to eliminate unreasonable to place on javascript grammar, thereby improving the safety of the code, the compiler efficiency, increase the operating speed and better compatibility with the new version javacscript future.

       Strict mode is mainly made improvements to the following unreasonable, include: disabling with keywords, to prevent accidental global variable, the function of this point is no longer a global default to prevent duplicate names function parameters, object properties to prevent duplicate names, and more safe to use eval and so on.

       Enable strict mode is very simple, mainly to add the following code in the code:

      "use strict";

       A few best practices to follow when using strict mode.

  1. Do not enable strict mode in the big picture

            For compatibility with some old code that is best not to globally enable strict mode, if you want to give a lot of code to set strict mode, you can put code in a function executed immediately and enable strict mode in the beginning of the function, the sample code as follows:

                  (function(){

                     "use strict"

                     // ..........

                     })();

        2. If you want to write more strictly javascript code, you can use the javascript code checking tool. The most popular is the main tool to check JSLint and JSHint.

 

 

     

 

Guess you like

Origin www.cnblogs.com/cby-love/p/12079878.html