How to Compile High-Quality Javascript code)

Source: Internet
Author: User

In his new book (Javascript Patterns), Stoyan Stefanov introduces many techniques for writing high-quality code, such as avoiding using global variables and using a single var keyword, cyclic pre-storage length and so on.

This article not only considers code optimization, but also the code design stage, including writing API documents, review of colleagues, and using JSLint. These habits can help you write higher quality, easier to understand, and more maintainable code (so that your code will be proud of you after many years ).

Compile maintainable code

Fixing software bugs requires a lot of effort. Especially after the code has been released, the maintenance cost increases with the increase of time. When you discover a BUG, you can fix it immediately. At this time, your code is still hot, and you do not need to recall it, because it was just written. But when you do other tasks and almost forget the code, you need:

  • Learn and understand questions again
  • Understand how the code solves the problem

Another problem is that in a large project or company, it is often because the person who solves the BUG is not the person who produces the BUG, and is not the person who discovers the BUG. Therefore, reducing the time needed to understand the code is the most important issue. Whether the code is written by yourself or by other members of the team, because we all want to create new and interesting things, rather than maintaining old code.

Another common problem in development is that it usually takes more time to read code than to write code. Sometimes it may take you an entire afternoon to write your code. This code can work at the time, but with the development, other things have changed a lot. At this time, you need to review and edit the code yourself. For example:

  • There are bugs not solved
  • Added new features
  • The program needs to run in the new environment (for example, a newly published browser)
  • Code Problems
  • The code needs to be rewritten because it modifies the architecture and even uses another language.

For these reasons, it may take weeks to read the code you wrote in the afternoon. Therefore, writing maintenance code is critical to the success of the software.

The maintenance code includes:

  • Readability
  • Continuity
  • Foresight
  • It seems to be written by a person.
  • Documentation available
Minimal global variables

Javascript uses functions to define the scope. A variable declared inside the function is invisible outside. Therefore, global variables are declared outside of any function or are not declared.

In Javascript, there is an accessible global object outside of any function. Every global variable you create is an attribute of this object. In a browser, Windows is usually used to refer to this global variable for convenience. The following code creates a global variable:

myglobal = "hello"; // antipatternconsole.log(myglobal); // "hello"console.log(window.myglobal); // "hello"console.log(window["myglobal"]); // "hello"console.log(this.myglobal); // "hello
Global Variables

The problem with global variables is that they are shared in all your code or on one page. They are under the same namespace, which usually results in a variable name conflict-two variables with the same name, but they are actually different in use.

Generally, some other people's code needs to be introduced in some pages, such:

  • Third-party JS Library
  • Script of advertising partner
  • Third-party user behavior analysis or statistical scripts
  • Different components and buttons

Add a third-party component to define a global variable: result. Then, a global variable result is defined in your program. The final result will overwrite the result before the vertex, so that the third-party scripts will stop working.

Therefore, to be friendly to other scripts, the fewer global variables on a page, the better. There will be some methods later to show you how to reduce global variables, such as using namespaces or self-executed anonymous functions, but the best way to avoid global variables is to use the var keyword to declare variables.

Because of the two features of javascript, it is very easy to create a global variable. First, you can use a variable that is not even declared. Second, in javascript, all undeclared variables will become an attribute of the Global Object (just like a declared global variable ). Let's take a look at this example:

function sum(x,y){     result = x + y;     return result;}

In this Code, the result is used without being declared, and this Code can work well, but after calling this function, A global variable named result will be added, which is the root cause of all problems.

To solve this problem, use var:

  • 8 pages in total:
  • Previous Page
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • Next Page

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.