ES6
let
` // 块级作用域,不允许重复声明,没有变量提升
// 暂存性死区
let name = "纳西妲"
function fn() {
console.log(name)
let name =
"局部作用域里调用之后再用let声明变量,就会出现暂存性死区,会报错"
}
fn()
// 不与顶层对象挂钩
var names = "挂载在window上"
console.log(window.names)
let notMsg = "不关联"
console.log(window.notMsg)
const
`
const name = 1;
const obj = Object.freeze({
name: "张三",
age: 18,
child: {
name: "冻不住",
},
});
解构赋值
` // 数组解构
let arr = [1, 2, 3]
let [a, b, c] = arr
let [d, [e, f], g] = [1, [2, 3], 5]
let [x = 999] = []
let [y = 999] = [666]
console.log(y)
// 对象解构
const obj = {
name: "张三",
age: 18,
child: { list: [6, 7, 8] },
}
let {
name,
age,
child: { list },
} = obj
console.log(name, age, list)
// 字符串解构
let { length } = "nan"
console.log(length)
模板字符串
` // ${} 大括号里面可以放表达式或变量作为字符串`
字符串与数值的扩展
` let myName = "jack";
myName.includes("a", 1);
myName.startsWith("j", 0);
myName.endsWith("j", 1);
myName.repeat(3);
Number.isFinite("100");
Number.isNaN(NaN);
Number.isInteger(100.0);
function isEqual(x, y) {
return Math.abs(x - y) < Number.EPSILON;
}
console.log(isEqual(0.1 + 0.2, 0.3));
Math.trunc(1.8);
Math.trunc(-1.8);
Math.sign(-100);
Math.sign(+100);
Math.sign(-0);
Math.sign(+0);
Math.sign("haha");