1、let
生命一個只在let所在代碼塊內生效的變量
2、conset
聲明一個只讀的常量。一旦聲明,常量的值就不能改變。
3、變量的解構賦值
let [foo, [[bar], baz]] = [1, [[2], 3]];
let [x, y = 'b'] = ['a', undefined];
let { foo, bar } = { foo: "aaa", bar: "bbb" };
let arr = [1, 2, 3];let {0 : first, [arr.length - 1] : last} = arr;first // 1last // 3
const [a, b, c, d, e] = 'hello';
[[1, 2], [3, 4]].map(([a, b]) => a + b);
// [ 3, 7 ]
let {length : len} = 'hello';
len // 5
for (let [key,value] of map) { }
const { SourceMapConsumer, SourceNode } = require("source-map");//模塊加載
解構賦值的規則是,只要等號右邊的值不是對象或數組,就先將其轉爲對象。ES6 的規則是,只要有可能導致解構的歧義,就不得使用圓括號。
4、字符串擴展
codePointAt(0)方法的參數,是字符在字符串中的位置(從0開始),返回一個字符的碼點。
String.fromCodePoint()識別32位的UTF-16字符碼點
字符串可以被for...of
循環遍歷
at(0)
方法,可以識別
Unicode 編號大於0xFFFF
的字符,返回正確的字符
includes():返回布爾值,表示是否找到了參數字符串。startsWith():返回布爾值,表示參數字符串是否在原字符串的頭部。endsWith():返回布爾值,表示參數字符串是否在原字符串的尾部。
let s = 'Hello world!';
s.startsWith('world', 6) // true
s.endsWith('Hello', 5) // true
s.includes('Hello', 6) // false
上面代碼表示,使用第二個參數n
時,endsWith
的行爲與其他兩個方法有所不同。它針對前n
個字符,而其他兩個方法針對從第n
個位置直到字符串結束。
repeat(0)
方法返回一個新字符串,表示將原字符串重複n
次。
字符串補全
'x'.padStart(5, 'ab') // 'ababx'
'x'.padStart(4, 'ab') // 'abax'
'x'.padEnd(5, 'ab') // 'xabab'
'x'.padEnd(4, 'ab') // 'xaba'
``.trim() //去除多餘的換行和空格
5、正則擴展
new RegExp(/abc/ig, 'i').flag
/\u{20BB7}/u.test('