快速瀏(liu)覽(lan) ES2015、ES2016、ES2017、ES2018 及以(yi)后(hou)的(de) JavaScript 新(xin)特性
function fn () {
let x = 0
if (true) {
let x = 1 // 只(zhi)在這個`if`里面
}
}
const a = 1
let
是新的 var
。 常量(const
) 就像 let
一樣工作,但(dan)不能重(zhong)新分配。
請參閱:
const message = `Hello ${name}`
const str = `
hello
world
`
模(mo)板和多行字符串。 請參(can)閱(yue):
let bin = 0b1010010
let oct = 0o755
請參閱:
const byte = 2 ** 8
// 同: Math.pow(2, 8)
"hello".repeat(3)
"hello".includes("ll")
"hello".startsWith("he")
"hello".padStart(8) // " hello"
"hello".padEnd(8) // "hello "
"hello".padEnd(8, '!') // hello!!!
"\u1E9B\u0323".normalize("NFC")
Number.EPSILON
Number.isInteger(Infinity) // false
Number.isNaN("NaN") // false
Math.acosh(3) // 1.762747174039086
Math.hypot(3, 4) // 5
Math.imul(Math.pow(2, 32) - 1, Math.pow(2, 32) - 2) // 2
// 返回(hui)一個真實的數組(zu)
Array.from(document.querySelectorAll("*"))
// 類似于 new Array(...),但沒(mei)有特殊的單(dan)參數行為(wei)
Array.of(1, 2, 3)
請參閱:
class Circle extends Shape {
constructor (radius) {
this.radius = radius
}
getArea () {
return Math.PI * 2 * this.radius
}
expand (n) {
return super.expand(n) * Math.PI
}
static createFromDiameter(diameter) {
return new Circle(diameter / 2)
}
}
原型的語法糖。 請參閱:
javascript 默認字段是公共的(public
),如果需要注明私有,可以使用(#
)
class Dog {
#name;
constructor(name) {
this.#name = name;
}
printName() {
//只能在(zai)類內部(bu)調用私有字段
console.log(`你的名字是${this.#name}`)
}
}
const dog = new Dog("putty")
//console.log(this.#name)
//Private identifiers are not allowed outside class bodies.
dog.printName()
class ClassWithPrivate {
static #privateStaticField;
static #privateStaticFieldWithInitializer = 42;
static #privateStaticMethod() {
// …
}
}
new Promise((resolve, reject) => {
if (ok) { resolve(result) }
else { reject(error) }
})
用于異(yi)步編程。 請參閱(yue):
promise
.then((result) => { ··· })
.catch((error) => { ··· })
promise
.then((result) => { ··· })
.catch((error) => { ··· })
.finally(() => {
/* 獨(du)立(li)于(yu)成功/錯誤的(de)邏(luo)輯(ji) */
})
當承(cheng)諾被(bei)履行(xing)或被(bei)拒絕時,處理(li)程序(xu)被(bei)調用
Promise.all(···)
Promise.race(···)
Promise.reject(···)
Promise.resolve(···)
async function run () {
const user = await getUser()
const tweets = await getTweets(user)
return [user, tweets]
}
async
函(han)數(shu)是使用(yong)函(han)數(shu)的另一種方式。
請參閱:
const [first, last] = ['Nikola', 'Tesla']
let {title, author} = {
title: 'The Silkworm',
author: 'R. Galbraith'
}
支(zhi)持匹配(pei)數組和對象。 請參閱:
const scores = [22, 33]
const [math = 50, sci = 50, arts = 50] = scores
// Result:
// math === 22, sci === 33, arts === 50
可以在解(jie)構數組或對象(xiang)時分(fen)配默認值
function greet({ name, greeting }) {
console.log(`${greeting}, ${name}!`)
}
greet({ name: 'Larry', greeting: 'Ahoy' })
對象和數組的解(jie)構也(ye)可以在函(han)數參數中(zhong)完成
function greet({ name = 'Rauno' } = {}) {
console.log(`Hi ${name}!`);
}
greet() // Hi Rauno!
greet({ name: 'Larry' }) // Hi Larry!
function printCoordinates({ left: x, top: y }) {
console.log(`x: ${x}, y: ${y}`)
}
printCoordinates({ left: 25, top: 90 })
此示例將 x
分配給 left
鍵的值
for (let {title, artist} of songs) {
···
}
賦值表達(da)式也在循環中(zhong)工作
const { id, ...detail } = song;
使用 rest(...)
運算符單獨提取一(yi)些鍵和對象中的剩余鍵
const options = {
...defaults,
visible: true
}
const options = Object.assign(
{}, defaults,
{ visible: true })
對象擴展運(yun)算(suan)符(fu)允許您(nin)從其他對象構建(jian)新對象。 請參閱:
const users = [
...admins,
...editors,
'rstacruz'
]
const users = admins
.concat(editors)
.concat([ 'rstacruz' ])
擴展運算(suan)符允許您以相同的方式構建新數(shu)組(zu)。 請參閱:
function greet (name = 'Jerry') {
return `Hello ${name}`
}
function fn(x, ...y) {
// y 是一個(ge)數組
return x * y.length
}
fn(...[1, 2, 3])
// 與 fn(1, 2, 3) 相同(tong)
Default(默認(ren)), rest, spread(擴展)。 請參閱(yue):
setTimeout(() => {
···
})
readFile('text.txt', (err, data) => {
...
})
arr.map(n => n*2)
// 沒有(you)花括號 = 隱式返回
// 同: arr.map(function (n) { return n*2 })
arr.map(n => ({
result: n*2
}))
// 隱(yin)式返(fan)回對象(xiang)需要(yao)在對象(xiang)周圍加上括(kuo)號
類似函數,但保留了 this
。
請參閱:
function log(x, y = 'World') {
console.log(x, y);
}
log('Hello') // Hello World
log('Hello', 'China') // Hello China
log('Hello', '') // Hello
function foo({x, y = 5} = {}) {
console.log(x, y);
}
foo() // undefined 5
function foo() {}
foo.name // "foo"
function foo(a, b){}
foo.length // 2
module.exports = { hello, bye }
同下:
module.exports = {
hello: hello, bye: bye
}
請參閱:
const App = {
start () {
console.log('running')
}
}
// 同(tong): App = { start: function () {···} }
請參閱:
const App = {
get closed () {
return this.status === 'closed'
},
set closed (value) {
this.status = value ? 'closed' : 'open'
}
}
請參閱:
let event = 'click'
let handlers = {
[`on${event}`]: true
}
// 同: handlers = { 'onclick': true }
請參閱:
const fatherJS = { age: 57, name: "張三" }
Object.values(fatherJS)
// [57, "張三(san)"]
Object.entries(fatherJS)
// [["age", 57], ["name", "張三"]]
import 'helpers'
// 又名: require('···')
import Express from 'express'
// 又名(ming): const Express = require('···').default || require('···')
import { indent } from 'helpers'
// 又名: const indent = require('···').indent
import * as Helpers from 'helpers'
// 又名: const Helpers = require('···')
import { indentSpaces as indent } from 'helpers'
// 又名: const indent = require('···').indentSpaces
import
是新的 require()
。
請參閱:
export default function () { ··· }
// 又名: module.exports.default = ···
export function mymethod () { ··· }
// 又(you)名: module.exports.mymethod = ···
export const pi = 3.14159
// 又名: module.exports.pi = ···
const firstName = 'Michael';
const lastName = 'Jackson';
const year = 1958;
export { firstName, lastName, year };
export * from "lib/math";
export
是新的module.exports
。
請參閱:
as
關鍵字重命名import {
lastName as surname // 導入重命名
} from './profile.js';
function v1() { ... }
function v2() { ... }
export { v1 as default };
// 等(deng)同于 export default v1;
export {
v1 as streamV1, // 導出重命(ming)名(ming)
v2 as streamV2, // 導出重(zhong)命名
v2 as streamLatestVersion // 導出重(zhong)命名
};
button.addEventListener('click', event => {
import('./dialogBox.js')
.then(dialogBox => {
dialogBox.open();
})
.catch(error => {
/* Error handling */
})
});
引入 import()
函數
const main = document.querySelector('main')
import(`./modules/${someVariable}.js`)
.then(module => {
module.loadPageInto(main);
})
.catch(err => {
main.textContent = err.message;
});
為 import
命令添加了一個元屬性 import.meta
,返回當前模(mo)塊(kuai)的元信息
new URL('data.txt', import.meta.url)
Node.js 環境中,import.meta.url
返回的總是本地路徑,即 file:URL
協議的字符串,比如 file:///home/user/foo.js
import json from "./package.json" assert {type: "json"}
// 導入 json 文(wen)件(jian)中的所有對象(xiang)
const json =
await import("./package.json", { assert: { type: "json" } })
function* idMaker () {
let id = 0
while (true) { yield id++ }
}
let gen = idMaker()
gen.next().value // → 0
gen.next().value // → 1
gen.next().value // → 2
情況很復雜。 請參閱:
let fibonacci = {
[Symbol.iterator]() {
let pre = 0, cur = 1;
return {
next() {
[pre, cur] = [cur, pre + cur];
return { done: false, value: cur }
}
}
}
}
for (var n of fibonacci) {
// 在 1000 處(chu)截(jie)斷序列
if (n > 1000) break;
console.log(n);
}
用于迭代生(sheng)成器(qi)和數組。 請(qing)參閱:
var gen = {};
gen[Symbol.iterator] = function* () {
yield 1;
yield 2;
yield 3;
};
[...gen] // => [1, 2, 3]
Generator
函數賦值給 Symbol.iterator
屬性,從而使得 gen
對象具有了 Iterator
接口,可以被 ...
運算符遍歷了
function* gen() { /* some code */ }
var g = gen();
g[Symbol.iterator]() === g // true
gen
是一個 Generator
函數,調用它會生成一個遍歷器對象g
。它的 Symbol.iterator
屬性,也是(shi)一個(ge)遍(bian)歷器對象生成(cheng)函數,執行后返回它自己