js 红宝石3.5~3.8

110 阅读1分钟

+-

+转为正数 -转为负数

!! 转为布尔值在取反

console.log(!!"blue"); // true 
console.log(!!0); // false 
console.log(!!NaN); // false 
console.log(!!""); // false 
console.log(!!12345); // true

逗号操作符

在赋值时使用逗号操作符分隔值,最终会返回表达式中最后一个值

let num1 = 1, num2 = 2, num3 = 3; 
let num = (5, 1, 4, 8, 0); // num 的值为 0 

with语句 3.6.9

严格模式使用with会报错

let qs = location.search.substring(1); 
let hostName = location.hostname; 
let url = location.href; 
上面代码中的每一行都用到了 location 对象。如果使用 with 语句,就可以少写一些代码:
with(location) { 
 let qs = search.substring(1); 
 let hostName = hostname; 
 let url = href; 
}