Best practice javascript

1. Code of semantics, like writing a story describing the same code.

  Blank lines 4 spaces

  Variable name at face value, does not limit the length of the hump wording

  Boolean value isChange, named

  To the package name, main.utils.init /main.date.tatal (Methods name / packet / implemented), semantic

  Method name verb + Name function getAllParams () {}

2. Object ownership, free to change other people's methods and classes, objects, cause unknown errors, all changes are inherited, on the basis of copies. Do not make modifications to the original method of writing

  Everyone implement a class, we have the power to modify the methods and classes, others should not.

  main.utils.init /main.date.tatal (Methods name / packet / implemented), semantic

  Reduce global variables, all variables should be defined in, bags, blocks, should not be open to write directly. (Methods which can even destroy, reduce memory overhead and modified, can be directly track the package emerging issues)

3. Reduce the complexity of the event, the query can be less upward several times, it will be faster. Especially the extraction cycle inside, variables, can reduce the overhead read more.

  var name = this.tatal.getDate().name;

  var age = this.tatal.getDate().age;

 

     rewrite

var date = this.tatal.getDate()

var age = date .age;
var name = date.name;

 

  Dom operation to reduce the complexity of the event, all the updates modifications to the previous top

  

for(var i = 0; i< 100; i++){
    dom = create('div');
    dom.append('<div>sds</div>');
    $('main').append(dom);              
}

  Should be reduced to modify dom, generated once an entire DOM, to fill mian, 100 rather than filling update dom

 

 

4. A small amount of circulation, direct write, do not cycle. because

 The for loop will form a local scope, contains a reference to the external pointer variable, more memory, slow, a small amount of direct write cycle.

 

The reduction coupling

Minimize dependence, to achieve a functional between files and folders, just on its own to modify itself, so that once you delete a function, only need to modify one. For example: In order to modify the click event, you should only need to click on the delete events directly on the line. Rather than have to modify the html and js and css.

  1.html and js Do not confuse write, $ ( 'div'). Append ( '<div> sdd </ div>') such an approach is not recommended. html html only to realize, or debug a problem for both sides.

  2. <div style = "width: 22px"> write does not support, you should try to update with the updated class style, so all in all css css-tone layer to achieve. Instead of changing for the better in several places, I do not know where the problem is

 

 

6. literal wording of the object and the array

No new Object and new Array () directly

{Name: 'wdds', age: 1000} and [ 'age', 'dsd', 'sjdks'] write overhead is relatively small

 

7. Switch faster than a do while, do while faster than while

 

Other: cpu registers which contains, is the fastest storage structure. Real-time calculation of the variable value.

       Heap and stack are stored in the memory inside, (ram), which is stored in the heap new Object () out of the entity. References to memory.

    Save the suite is the basic variable types and reference pointer. Back and forth movement of the stack, the stack pointer migration, can clear.

 

    Objects (so new Object () should be out in time be destroyed, or take up too much memory in the heap. Read slower, the program card.

        Defined global variables are not destroyed in a timely manner, all inside the stack, not be destroyed, so much memory, so do not define global variables. )

 

bugList:

1.onclick event will cause problems in the closure click events, cause memory leaks, how to release this part of the variable

Guess you like

Origin www.cnblogs.com/chenyi4/p/12089914.html