优良的JS代码应该是什么样式、风格?
1.空白字符
- 不要混用空格和制表符(空格用" "表示,制表符用"\t"表示)
- 在编码之前,确定好使用 软缩进(空格) 还是 真实的制表符(\t) (推荐将Editor的缩进设置成两个字符)
- 确保‘显示隐藏字符(show invisibles)’选项可用。如此好处:
- 强制一致性
- 消除行尾的空白字符
- 消除空行
- 易于识别差异
2.括号、换行符、大括号
if/else/while/try
//不良的书写风格 if(condition) doSomeWork(); while(condition) i++; for(var i=0;i<10;i++); //优良的书写风格 if (condition) { //代码 } while ( condition ) { //代码 } for ( var i = 0; i < 100; i++ ) { //代码 } //升级版优良书写风格 var i, length = 100; for ( i = 0;i < length; i++ ) { //代码 } var i = 0, length = 100; for( ; i < length; i++ ) { //代码 } var value; for( value in object ) { //代码 } if( true ) { //代码 } else { //语句 } //不良写法 var x=y+5; //优良写法 var x = y + 5;
3.引号
在一个项目中主要使用一种引号!!!要么双引号,要么单引号。
4.行尾和空白
空白字符会使代码的差异与变更无法辨识。应该使用编辑器将多余的空格去掉。
5.类型检查
//String typeof variable === 'string' //Number typeof variable === 'number' //Boolean typeof variable === 'boolean' //Object typeof variable === 'object' //null variable === null //undefined或null variable == null
6.类型转换
- 在语句开始强制转换
//不良风格 const totalScore = this.reviewScore + ''; //优良风格 const totalScore = String(this.reviewScore);
- 对Number类型使用parseInt()的时候,加入基数用于转换
const inputValue = '4'; //不良风格 const val = new Number(inputValue); const val = +inputValue; const val = inputValue >> 0; const val = parseInt(inputValue); //优良风格 const val = Number(inputValue); const val = parseInt(inputValue,10);
- 用Boolean()进行类型转换
const age = 0; //不良风格 const hasAge = new Boolean(age); //优良风格 const hasAge = Boolean(age); const hasAge = !!age;
7.条件求值
//当数组长度不为空时 //不良写法 if ( array.length > 0) { //代码 } //优良写法(逻辑测试为真) if ( array.length ){ } //当数组长度为空时 //不良写法 if ( array.length === 0) { //代码 } //优良写法(逻辑测试为真) if ( !array.length ){ } //检查字符串是否为空时 //不良写法 if ( string !== '') { //代码 } //优良写法(逻辑测试为真/假) if ( string ) { // } if ( !string ) { // } //检查引用是否有效 //不良写法 if ( foo === true ) { // } //优良写法(逻辑真假) if ( foo ) { } //引用可能是null undefined 但是不会是 false '' 0 //不良写法 if ( foo === null || foo === undefined) { // } //优良写法 if ( foo == null ){ // } //尽量减少三元表达式的嵌套 //糟糕的写法 return x === 0 ? 'Sunday' : x === 1 ? 'Monday' : 'Tuesday' ; //稍微好一点 if ( x === 0 ) { return 'Sunday'; } else if ( x === 1 ) { return 'Monday'; } else { return 'Tuesday'; } //更好一点的写法 switch ( x ) { case 0: return 'Sunday'; case 1: return 'Monday'; default: return 'Tuesday'; }
8.命名
- 尽量不用单个字母命名
//费解的命名 function q () { // } //浅显的命名(推荐) function query () { // }
- 对象 函数 实例 使用驼峰命名
//不良命名 const OBJECT = {}; const this_is_object = {}; function aaa() { // } //优良命名 const thisIsObject = {}; function thisIsFunction () { // }
- 构造函数 使用Pascal命名
//不良命名 function user (options) { // } const newuser = new user(); //优良命名 function User (options) { // } const newUser = new User();
- 命名私有属性的时候在前面加上"_"
//不良命名 this._firstName_ = 'Panda'; this.firstName = 'Panda'; //优良命名 this._firstName = 'Panda';9.eval()
不是很推荐eval , 因为它很容易被恶意注入,而且它的技巧性太强,运行速度也慢...看个人能力吧!极其NewBalance的人随意。
//object console.log(typeof eval(new String('1+!'))); //string console.log(eval(new String('1+1'))); //2 console.log(eval('1+1')); //number console.log(typeof eval('1+1')); //2 var expression = new String('1+1'); console.log(eval(expression.toString()));
10.严格模式
建议在项目开始就使用严格模式。
可以先在JS文件或者<script>标签内输入
'use strict'
引号也需要
来进入严格模式
如果想以函数为单位启用严格模式
function foo () { 'use strict'; // }
- 对已有代码启用严格模式,会出现问题
- 谨慎打包
- 严格模式中的变量必须声明
- eval()函数的行为更加清晰
- 在被求值的字符串中声明的变量,不会添加到外围作用域中
- 严格模式被禁止的东西
- with
- 懒散模式中,前面带有0的数字会被认为是八进制。严格模式下这样使用会报错。
//Uncaught SyntaxError: Octal literals are not allowed in strict mode.
11.JSHint
可以启用JSHint来检查JS文件。
如果已经安装了 npm 那么可直接在命令行输入
npm install jshint -g 回车
即可
我在桌面上创建了一个test.html文件随性输入一些代码 然后进入jshint测试
测试命令是在文件所在目录下输入 jshint test.html
后面是文件名即可,也可以单独测试js文件