JS中禁止连续赋值和使用 new Number/String/Boolean。eslint: no-multi-assign/no-multi-assign

// bad
(function test() {
  let a = b = c = 1; // 相当于 let a = (b = (c = 1));
})();

console.log(a); // throws ReferenceError
console.log(b); // 1
console.log(c); // 1

// good
(function test() {
  let a = 1;
  let b = a;
  let c = a;
})();

console.log(a); // throws ReferenceError
console.log(b); // throws ReferenceError
console.log(c); // throws ReferenceError

本例的结果是 let 仅对 a 起到了预想效果,b 和 c 都成了全局变量, eslint: no-multi-assign

// bad
const num = new Number(0);
const str = new String('foo');
const bool = new Boolean(false);
console.log(typeof num, typeof str, typeof bool); // => object, object, object
if (num) { // true(对象相当于 true)
}
if (bool) { // true(对象相当于 true)
}

// good
const num = 0;
const str = 'foo';
const bool = false;
console.log(typeof num, typeof str, typeof bool); // => number, string, boolean
if (num) { // false(0 相当于 false)
}
if (bool) { // false
}

不要使用 new Number/String/Boolean。eslint: no-new-wrappers

使用 new Number/String//Boolean 声明不会有任何好处,还会导致变量成为 object 类型,可能引起 bug。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果还有`/* */`和`<%-- --%>`这样的多行注释,可以使用一个标志变量来判断当前是否在注释,如果在注释,则跳过当前行,否则再进行空格和单行注释的过滤。 以下是示例代码: ```java import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class ReadFile { public static void main(String[] args) { try { File file = new File("your_file_path.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); String line; boolean inComment = false; // 是否在注释 while ((line = br.readLine()) != null) { // 过滤空格和单行注释 if (!inComment && !line.trim().startsWith("//") && !line.trim().equals("")) { System.out.println(line); } // 处理多行注释 if (line.contains("/*")) { if (!line.contains("*/")) { inComment = true; } } if (line.contains("*/")) { if (!line.contains("/*")) { inComment = false; } } // 处理JSP注释 if (line.contains("<%--")) { if (!line.contains("--%>")) { inComment = true; } } if (line.contains("--%>")) { if (!line.contains("<%--")) { inComment = false; } } } br.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在代码,如果发现`/*`或`<%--`开头的多行注释,则将标志变量`inComment`设为`true`,并跳过当前行。如果发现`*/`或`--%>`结束的多行注释,则将标志变量`inComment`设为`false`,表示当前已经不在注释。在处理单行注释时,只有当`inComment`为`false`时才进行过滤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值