Recognize the status of JavaScript and write reasonable JavaScript code

Source: Internet
Author: User

As a front-end programmer, be sure to recognize the status of JavaScript, not to be confused by its chaotic characteristics. JavaScript is primarily used to manipulate and readjust the DOM, by modifying the DOM structure to achieve the purpose of modifying the page's effect.

Use this central idea to guide all future JS learning, including its framework.

The so-called reasonableness is justified. So what are the principles that reasonable JavaScript code usually follows? I think at least to follow the following points:

Meaningful naming

Whether you choose the name of the camel or the Hungarian nomenclature, it is the most important and basic rule to know the meaning. Also avoid ambiguous naming.

For example, the variable "current Time"

var D; bad naming

var currenttime a good name

Each function is only one thing to do

One thing each function does is avoid putting all of the business logic in the same function, and the benefit is that the code is simpler, more readable, easier to maintain, and reusable.

Reasonable transfer of parameters

Take a simple example: Good funcation  querystudent (Json student) {      ...} bad arguments funcation  querystudent (Id,name,...), and so on ...}. * * * {............      }

A reasonable DOM
reasonable. var  Phaseobject =  = document.getElementById (' northleftphase ')}phaseobject.northleftphase.src = ' http://phase.com/n/left.png '; unreasonable document.getElementById (' northleftphase '). src = ' http://phase.com/ N/left.png ';

A reasonable comment
language Refinement, the use of standardized annotation mode, to prevent multiple meanings, often updated. Principle, not all variable functions need to be commented, generally note which are more jerky algorithms. Refining the Notes//Selector the Elements varfindelements = window. Findelements =function(exp) {...}...........} Non-Concise notes//Selector the Elements by getElementById & Getelementsbytagn ....Var findelements= window. Findelements =function(exp) {...}...........} Comments! =yes, subtitles.//Find [ID = A] varOA = document.getElementById (' A '); not good./*Found through the document.getElementById*/ varOA = document.getElementById (' A ');

Reasonable typesetting and indentation (indentation)

4 spaces are often used as a unit of indentation layout. The exact explanation of indentation is not specified in detail (Space vs. tab). A tab is equal to n spaces (depending on the editor, Eclipse defaults to a tab character of 4 characters).

Line length

Try to avoid a line that is longer than 80 characters because many terminals and tools cannot handle it well.

Note: Since the left and right sides of the Eclipse development tools workspace are occupied by non-code editors, it is recommended that the code length of each line not exceed 70 characters.

Line break (wrapping Lines)
      • When an expression cannot fit within a line, it can be broken according to the following general rules:
      • • Disconnect after a comma;
      • • Break in front of an operator;
      • • Prefer a higher-level (higher-level) break rather than a lower-level (lower-level) disconnection;
      • • The new line should be aligned with the beginning of the same level expression on the previous line.
Modular programming
    • Easy to manage;
    • improve development efficiency;
    • Reduce the number of error-changing code;
    • Easy to maintain;
    • Improve code readability;
    • Easy to build large projects.

Recognize the status of JavaScript and write reasonable JavaScript code

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.