- js不区分整数和浮点数,统一用Number表示,
- js'=='比较会自动转换类型,会产生奇怪结果,'==='不会转换比较类型,如果不一致返回false,因此js判断始终用'==='
-
`` 保留换行,也可以有变量的占位符
var name = '小明'; var age = 20; var message = `你好, ${name}, 你今年${age}岁了!`;
-
用let替代var可以申明一个块级作用域的变量
(function a() { for (var i=0; i<100; i++) { } console.log(i)//100 })(); (function b() { for (let i=0; i<100; i++) { } console.log(i)//i is not defined })();
- JSON
var xiaoming = { name:'lby', gender:'boy', age:null, height:null, }; var filter = function (key, value) { if(value !==null) return value; }//过滤null值 console.log(JSON.stringify(xiaoming,filter))//{"name":"lby","gender":"boy"} console.log(JSON.stringify(xiaoming))//{"name":"lby","gender":"boy","age":null,"height":null}
var obj = JSON.parse('{"name":"lby","age":25}', function (key, value) { if (key === 'name') { return value + 'sssssss'; } return value; }); console.log(JSON.stringify(obj));//{"name":"lbysssssss","age":25}
- 解构赋值
//数组 let [x, y, z] = ['a', 'b','c']; console.log(x,y,z) //a b c let [x1, [y1, z1]] = ['a', ['b','c']]; console.log(x1,y1,z1) //a b c
//对象 var p = {name:'lby',age:20,leg:{hair:50000}} var {name,age,leg:{hair}} = p console.log(name,age,hair)//lby 20 50000
用解构互换2个值 var x=1, y=2; [x, y] = [y, x]
函数多参数
var x = function ({a,b,c}) {
console.log(a,b,c)//aa bb cc
}
x({a:'aa',b:'bb',c:'cc'})