Use JSLint to improve JS code quality method sharing

Source: Internet
Author: User
Tags error code
This article mainly introduces the JSLint to improve the quality of JS code, the need for friends can refer to the advent of rich Web front-end applications, developers have to re-examine and attach importance to the ability of JavaScript language and use, discard the past that rely on "copy/paste" Common scripts complete a simple front-end task pattern. The JavaScript language itself is a weakly typed scripting language with a looser limit relative to the C + + or Java language, and all functions-centric functional programming ideas also provide developers with a more flexible syntax implementation. However, this flexibility is also a nightmare for beginners or inexperienced JavaScript developers, while bringing efficiency. Different forms of code style, hidden error code behavior, seriously affect the overall code readability and stability, become a WEB project one of the most common problems.   Therefore, we need an effective JavaScript code quality tool to detect and fix the problems implied in JavaScript code in a timely manner to ensure the quality of code delivery. JSLint, as a flexible and effective JavaScript code quality Detection Tool, allows users to specify the coding style conventions that meet their specific application development needs, and the style of the whole project is unified, this "rule" (options) driven working mode makes the JSLint can be applied to different code detection requirements. This article will first introduce the basic concepts and roles of jslint to the reader, explain its rule-based approach, and then use an example to illustrate its basic usage, and finally how to integrate JSLint into the ANT and Eclipse application process to show how the JSLint be used in daily development tasks.   What is JSLint JavaScript as a young, grammatically flexible and loosely formatted language, the confusion of code formats and the improper use of certain language features often make the final deliverable contain many unforeseen actions or errors resulting from coding style conventions, This habitual problem, if not pointed out and modified in time, is often reproduced in the iterative process of the project, which seriously affects the stability and security of WEB products. JSLint is precisely Douglas Crockford classmate to solve this kind of problem to create the tool, JSLint Besides can point out these unreasonable stipulation, also can mark the structure aspect problem. While JSLint does not guarantee that code logic is correct, it helps to identify errors and teach developers some good coding practices. It is worth mentioning that the JSLint tool itself is also a piece of JavaScript code, which is to verify the JavaScript code qualityThe amount of JavaScript script. JSLint's quality testing of JavaScript scripts mainly includes the following aspects:   Detection syntax error: for example, braces "{}" pairing error. • Variable definition specification: For example, the detection of undefined variables. • Code format specification: for example, the deletion of a semicolon at the end of a sentence. • Use detection for poor language features such as the use restrictions for eval and with. JSLint's version update has been active, and as of this writing, JSLint's latest version was published in 2010-10-10. Many mainstream code editors offer good extended support for jslint, including Eclipse, VS2008, and so on.   Currently, there are many JavaScript code detection tools similar to the JSLint function, including: YUI test, Firebug, MS Script Debugger, COMPANIONJS, etc. Most of them are in the form of browser plug-ins for JavaScript runtime detection and debugging, jslint and these tools are the important difference is that they pay more attention to static code format detection, which is the current hot agile development needs and advocated for continuous construction.   Understanding JSLint Rules jslint the principle of executing code quality detection is the set of rules set by user. JSLint the rule set provided by default contains the thought-poor development style that WEB developers have accumulated over the years, we can choose to build a set of specific rules based on the requirements of our own projects. JSLint will scan the JavaScript script according to it, and give the corresponding problem description information. The form of a rule is embodied in multiple sets of key-value pairs: [Param:option], a key to the rule name, or a value for the rule invocation. For example, the rule: "Plusplus:true" is not allowed to occur with the + + and--operators, and "undef:true" is not allowed to use undefined variables.   Because the JSLint tool is essentially a common JS script, its operation is naturally dependent on a JS run engine, which is loaded by the engine will produce a global JSLint function object in memory, the function object requires two input: source and options, the former used to refer to A string or array of strings that are parsed after the instrumented script file is resolved, which represents the user-defined rule option. If the options are empty, JSLint scans the source with its default rule.   The entire detection process is the jslint contained in the script An execution of the (source, options) function. When the specified source script detects passing under the options condition, JSLint returns True, otherwise returns false, and then a detailed error message can be obtained through the Jslint.errors object. Figure 1 shows the entire work process of the jslint.   Figure 1. JSLint working process Diagram as shown in the figure, the rule set is configured in three ways: 1. Modify the default rule directly by modifying the Jslint.js source code. 2. When the JSLint function is run, the options parameter is set at the same time, changing its rule option dynamically (the "overwrite"). This approach is useful for using the same set of custom rules for batch JS files. 3. By adding the annotation type rules to the header of the JS file to be detected, add a special rule (second overwrite) to the individual JS file that applies to the file code. This approach is useful for setting specific detection rules for different JS files, and is typically used to introduce some global variables into the file.

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.