JSLint and the JS encoding Style

Source: Internet
Author: User
Tags bitwise operators case statement

JSLint and the JS encoding Style
It may be more or less known that JSLint is a JavaScript code quality tool, a JavaScript syntax checker and a checker. It can analyze JavaScript problems and report its shortcomings. The problems found are often syntax errors, but not all. JSLint checks some code style conventions and structure problems. Next I will introduce some common sense of JSLint. The biggest problem with undefined variables and JavaScript Functions is the dependency on global variables, especially implicit global variables. JSLint expects all variables and functions to be declared before use or call. This method is used to determine whether it is an implicit global variable. If a JS file is dependent on a global variable or function defined elsewhere, you can include comments in this JS file to mark JSLint knowledge, this comment lists all global variables that do not require JSLint detection. /* Global GetByNameFn, myObj, myFn */Note that there is no blank before g. You can use any number of/* global */Annotations, but they must appear before the specified variables are used. Because JavaScript is a weak Dynamic Object Language, member elements can add any number of different types of attributes to objects at any time, this makes it impossible to determine whether the object's attribute name is correctly spelled during compilation. JSLint uses/* members */to check whether attributes are spelled incorrectly. You can use/* members */to annotate your script. JSLint checks the spelling of all attribute names based on the list. /* Members doTell, mercySakes, myGoodness, ohGo */semicolon JSLint expect that every statement except for, function, if, switch, try, and while will be followed by a semicolon, however, JSLint does not expect to see unnecessary semicolon line breaks. To make the Code look hierarchical, We will unconsciously use empty lines to layer the Code. However, JSLint suggests that you always wrap the lines after the operators, this can avoid many unnecessary errors. As we all know, commas can be used as separators or operators in JavaScript. However, JSLint expects the comma to be used as a separator rather than an operator (except in the initialization and increment of the for statement). It does not expect that some elements are omitted from the array literal, redundant commas should not be used. For example, they should not appear after the last element of the array literal or object literal, because they will be incorrectly parsed by Some browsers. The necessary code block JSLint does not expect you to omit the code block statement. That is to say, we recommend that you use curly brackets even if there is only one statement in the conditional judgment statement. The front-end declaration of variables in the scope everyone knows that there is no block scope in JavaScript, only the function scope, and the variables have the ability to improve the Declaration. Therefore, JSLint recommends that the front-end declaration in the scope, it is best to use a single statement variable declaration. The for in statement can be used to traverse the names of all attributes of an object, but it also traverses all member elements inherited from the prototype chain. So it is best to use the following code: for (var name in object) {if (object. hasOwnProperty (name) {}} although the Switch statement in JavaScript can penetrate the case statement, JSLint expects that there is one of the following statements before the next case or default statement: break, return, and throw. In JavaScript, The With statement is sometimes convenient, but JSLint does not expect the with statement. = JSLint does not expect to see the value assignment statement in the condition section of the if or while statement. if you really want to assign a value, enclose it in parentheses, as shown in the following code: if (a = B) {}= and! ===And! = Forced type conversion is performed before the comparison is executed. We recommend that you use ===and! = To replace = and! =. If you want to perform forced type conversion, use the simple format. (Foo! = 0) (foo) // we recommend that you use this simple method to implement (foo! = 0) (foo = 0 )(! Foo) // we recommend that you use this simple method to implement the (foo = 0) Tag JavaScript, which allows any statement to have a tag and has a separate namespace, however, JSLint expects that labels will only be used in the following statements that interact with the break statement, for example, switch while do for and other non-reachable code JSLint expects the return break continue or throw statement to be followed by a} chaotic positive and negative signs JSLint expect that ++ will not follow ++ or ++, similarly, a space with an improper position may change ++ to ++, which is difficult to detect, use parentheses to avoid confusion. ++ And -- JSLint are recommended to avoid using ++ and -- as this may lead to poorly understood code. Bitwise operators do not have integer types in JavaScript, but they have bitwise operators. bitwise operators convert their operation numbers from floating point to Integers before performing operations, therefore, bitwise operators in JavaScript are far less efficient than High void in other languages. void is a type in most languages. In JavaScript, void is a prefix operator that always returns undefined, JSLint does not expect to see void because it is confusing and useless. The constructor function and the new operator bit operator JSLint force convention that the constructor function must be named in uppercase, JSLint does not expect to see a function with uppercase letters to be called without a new prefix, and JSLint does not expect that the function name used with new is not starting with an uppercase letter. JSLint does not expect to see these encapsulation forms: new Number, new String, new Boolean JSLint does not expect to see new Object (using Object literal {} code) JSLint does not expect to see new Array (use Array literal [] 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.