Good programmers tutorial Web front-end JavaScript development skills to share

  Good programmers Web front-end tutorial Share JavaScript development skills , I believe know Web front-end of the small partners are familiar with , Javascript many extended features that it becomes more sharp, but will also give programmers the opportunity to create a more beautiful and lets the user likes site , then the novice should know what the necessary JavaScript development skills it? Here we look together.

 

  Although many developers are happy to celebrate JavaScript , but some people still see its dark side.

 

  Use a lot of javascript code Web pages will load slowly, excessive use of javascript allows web ugly and procrastination. Quickly how to use javascript to become a very hot topic.

 

  1 , as much as possible to keep the code simple

 

  May we all heard the N times the code simple problem. You may use as a developer in your code development process many times, but never in js forget this development.

 

  Try to add comments and spaces in development mode, so keep the code readability

 

  In a production environment before release to place whitespace and comments are deleted, and try abbreviations variables and method names

 

  Using third-party tools to help you achieve compression JavaScript .

 

  2 , thinking and then modify prototypes

 

  Add a new attribute to the object prototype is a common cause of script errors.

 

  yourObject.prototype.anotherFunction='Hello';

 

  yourObject.prototype.anotherMethod=function(){...}

 

  In the code above, all variables will be affected because they are inherited from yourObject . Such use can lead to unexpected behavior. It is proposed to delete a similar modified after use.

 

  yourObject.prototype.anotherFunction='Hello';

 

  yourObject.prototype.anotherMethod=function(){};

 

  test.anotherMethod();

 

  deleteyourObject.prototype.anotherFunction='Hello';

 

  deleteyourObject.prototype.anotherMethod=function(){};

 

  3 , DebugJavascript Code

 

  Even the best developers make mistakes. In order to maximize the reduction of similar errors, in your debugger to run your code, make sure you do not encounter any minor errors.

 

  4 , avoid Eval

 

  Your JS without eval can also work well when the method. eval allow access to javascript compiler. If a string is passed as a parameter to the eval , then its result may be performed.

 

  This will greatly reduce the performance of the code. Try to avoid using in a production environment eval .

 

  5 , minimizing DOM access

 

  DOM is the most complex API , code execution will make the process slower. Sometimes Web pages may not load or load incomplete. Best to avoid the DOM .

 

  6 , using javascript before first learning library javascript

 

  Internet is full of a lot of javascript libraries, many programmers often use js libraries without understanding the negative effects. It is strongly recommended that you learn the basics before you use third-party libraries JS code. Otherwise, you're ready unlucky.

 

  7 , do not use " the SetTimeOut " and " setInterval " Method as " the Eval " alternative

 

  setTimeOut("document.getID('value')",3000);

 

  在以上代码中document.getID(value)setTimeOut方法中被作为字符串来处理。这类似于eval方法,在每个代码执行中来执行一个字符串,因此会降低性能,因此,建议在这些方法中传递一个方法。

 

  setTimeOut(yourFunction,3000);

 

  8[]newArray();更好

 

  一个常犯的错误在于使用当需要数组的时候使用一个对象或者该使用对象的时候使用一个数组。但是使用原则很简单:

 

  “当属性名称是小的连续整数,你应该使用数组。否则,使用一个对象”–DouglasCrockford,JavaScript:GoodParts的作者.

 

  建议:

 

  vara=['1A','2B'];

 

  避免:

 

  vara=newArray();

 

  a[0]="1A";

 

  a[1]="2B";

 

  9、尽量不要多次使用var

 

  在初始每一个变量的时候,程序员都习惯使用var关键字。相反,建议你使用逗号来避免多余的关键字,并且减少代码体积。如下:

 

  varvariableOne='string1',

 

  variableTwo='string2',

 

  variableThree='string3';

 

  10、不要忽略分号“;

 

  这往往是大家花费数个小时进行debug的原因之一。


Guess you like

Origin blog.51cto.com/14249543/2412876