解构赋值
数组解构赋值
let [a,b,c] = [1,2,3];
解构不成功,值为undefined
let [head, ...tail] = [1, 2, 3, 4];<br>head // 1<br>tail // [2, 3, 4]
let [x, y, ...z] = ['a'];<br>x // "a"<br>y // undefined<br>z // []
等号右边不是数组,报错
// 报错<br>let [foo] = 1;<br>let [foo] = false;<br>let [foo] = NaN;<br>let [foo] = undefined;<br>let [foo] = null;<br>let [foo] = {};
Set结构解构
let [x, y, z] = new Set(['a', 'b', 'c']);<br>x // "a"
只要某种数据结构具有 Iterator 接口,都可以采用数组形式的解构赋值
默认值
let [foo = true] = [];<br>foo // true<br><br>let [x, y = 'b'] = ['a']; // x='a', y='b'<br>let [x, y = 'b'] = ['a', undefined]; // x='a', y='b'
对象的解构赋值
let { foo, bar } = { foo: "aaa", bar: "bbb" };<br>foo // "aaa"<br>bar // "bbb"
对象的解构与数组有一个重要的不同。数组的元素是按次序排列的,变量的取值由它的位置决定;而对象的属性没有次序,变量必须与属性同名,才能取到正确的值。
不将大括号写在行首,避免JavaScript将其解释为代码块
类似数组的对象都有一个length属性,因此还可以对这个属性解构赋值。<br><br>let {length : len} = 'hello';<br>len // 5
字符串的解构赋值
数值和布尔值的解构赋值
函数参数的解构赋值
圆括弧的用法没有细看,以后确实需要再详细研究。目前是统一不用
字符串的扩展
字符的Unicode表示法
codePointAt
String.fromCodePoint
字符串的遍历器接口
for (let codePoint of 'foo') {<br> console.log(codePoint)<br>}<br>// "f"<br>// "o"<br>// "o"
at()
'𠮷'.charAt(0) // "\uD842"
'𠮷'.at(0) // "𠮷"
normalize()
includes
startsWith
endsWith
repeat
padStart
padEnd
数值的扩展
Number.isFinite(), Number.isNaN()
Number.parseInt(), Number.parseFloat()
Number.isInteger()
Number.EPSILON
安全整数和Number.isSafeInteger()
Math对象的扩展
Math.trunc()
Math.sign()
Math.cbrt()
Math.clz32()
Math.imul()
Math.fround()
Math.hypot()
Math.expm1()
Math.exp()-1
Math.log1p()
Math.log(x+1)
Math.log10()
Math.log2()
三角函数方法
Math.sinh(x)
Math.cosh(x)
Math.tanh(x)
Math.asinh(x)
Math.acosh(x)
Math.atanh(x)
Math.signbit()
数组的扩展
Array.from()
Array.of()
数组实例的copyWithin()
数组实例的copyWithin()
数组实例的fill()
数组实例的entries(),keys()和values()
数组实例的includes()
Map结构的has方法,是用来查找键名的
Set结构的has方法,是用来查找值的
forEach(), filter(), every() 和some()都会跳过空位
map()会跳过空位,但会保留这个值
join()和toString()会将空位视为undefined,而undefined和null会被处理成空字符串。
for of
函数的扩展
默认参数
函数的 length 属性
可用来获取参数数量
对于默认值参数和rest参数,不参与计数
作用域
错误处理
function throwIfMissing() {<br> throw new Error('Missing parameter');<br>}<br><br>function foo(mustBeProvided = throwIfMissing()) {<br> return mustBeProvided;<br>}<br><br>foo()<br>// Error: Missing parameter
rest参数
形式为“...变量名”
rest 参数 只能是最后一个参数
函数的length属性,不包括 rest 参数
扩展运算符
扩展运算符的应用
合并数组
与解构赋值结合
实现了Iterator接口的对象
Map和Set结构,Generator函数
name 属性
箭头函数
ES6允许使用“箭头”(=>)定义函数。
注意点
函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象。
不可以当作构造函数,也就是说,不可以使用new命令,否则会抛出一个错误
不可以使用arguments对象,该对象在函数体内不存在。如果要用,可以用Rest参数代替
不可以使用yield命令,因此箭头函数不能用作Generator函数
嵌套的箭头函数
绑定this
尾递归
尾调用
尾递归优化
对象的扩展
属性的简洁表示法
属性名表达式
方法的 name 属性
Object.is()
Object.assign()
如果目标对象与源对象有同名属性,或多个源对象有同名属性,则后面的属性会覆盖前面的属性。
只拷贝源对象的自身属性(不拷贝继承属性),也不拷贝不可枚举的属性
常见用途
为对象添加属性
为对象添加方法
克隆对象
合并多个对象
为属性指定默认值
属性的可枚举性
属性的遍历
for...in
Object.keys(obj)
Object.getOwnPropertyNames(obj)
Object.getOwnPropertySymbols(obj)
Reflect.ownKeys(obj)
__proto__属性,Object.setPrototypeOf(),Object.getPrototypeOf()
Object.setPrototypeOf()(写操作)、Object.getPrototypeOf()(读操作)、Object.create()(生成操作)代替__prototype__
Object.setPrototypeOf()(写操作)、Object.getPrototypeOf()(读操作)、Object.create()(生成操作)代替
Proxy
源编程
可以用来实现 我要的继承
get(target, propKey, receiver)
set(target, propKey, value, receiver)
has(target, propKey)
deleteProperty(target, propKey)
ownKeys(target)
getOwnPropertyDescriptor(target, propKey)
defineProperty(target, propKey, propDesc)
preventExtensions(target)
getPrototypeOf(target)
isExtensible(target)
setPrototypeOf(target, proto)
apply(target, object, args)
construct(target, args)