Use JSLint to improve JS code quality method sharing
- 2020-03-30 00:55:32
What is a JSLint
Know the JSLint rules
Due to JSLint tool is essentially a common JS script, its operation is also naturally relies on a JS engine operation, it is after the engine load can produce a global JSLint in memory function object, the object function requires two input: the source and the options, the former is used to specify a script to be detected after the file is parsed generated string or an array of strings, the latter according to user-defined rules options. If the options are empty, JSLint scans the source using its default rules.
The entire detection process is an execution of the JSLINT (source, options) function contained in the script. JSLint returns true when the specified source script is checked in the options condition, false otherwise, and detailed error information can be obtained from the jslint.errors object. Figure 1 shows the entire working process of JSLint.
Figure 1. Schematic diagram of JSLint working process
< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201312/20131216154204364.jpg" >
As shown in the figure, rule sets can be configured in three ways:
1. Directly modify the default rules by modifying the jslint.js source code.
2. When JSLint function runs, set the options parameter at the same time and dynamically change the rule option (first overwrite). This approach applies to using the same set of custom rules for bulk js files.
3. Add a rule of annotation type to the header of the js file to be detected, and add a special rule (second overwrite) applicable to the code of a single js file. This approach is suitable for setting specific detection rules for different js files and is often used to introduce some global variables into the file.