Cheat Sheet
Cheat Sheet
ES6 cheatsheet
A quick reference cheatsheet of what's new in JavaScript for ES2015, ES2016, ES2017, ES2018
and beyond
# Getting Started
- Block-scoped - Template Strings Binary and octal literals
Let Interpolation
let bin = 0b1010010
let oct = 0o755
function fn () { const message = `Hello ${name}`
let x = 0
if (true) { Multi-line string
let x = 1 // only inside this `if`
const str = ` See: Binary and Octal Literals
}
} hello
the world
Exponential Operator
Const `
let is the new var. Constants (const) work just like Templates and multiline strings. See: template
let, but cannot be reassigned. See: Let and const strings Same as: Math.pow(2, 8)
New string methods The javascript default field is public (public), if you
class Circle extends Shape {
need to indicate private, you can use (#)
"hello".repeat(3)
Constructor
"hello". includes("ll")
"hello". startsWith("he") class Dog {
constructor (radius) { #name;
"hello".padStart(8) // "hello"
this.radius = radius constructor(name) {
"hello".padEnd(8) // "hello"
} this.#name = name;
"hello".padEnd(8, '!') // hello!!!
"\u1E9B\u0323".normalize("NFC") }
method printName() {
New Number Methods // Only private fields can be called inside
getArea () {
console.log(`Your name is ${this.#name}`)
return Math.PI *2 *this.radius
Number.EPSILON }
}
Number.isInteger(Infinity) // false }
Number.isNaN("NaN") // false
Call the superclass method
const dog = new Dog("putty")
New Math methods expand(n) {
//console.log(this.#name)
//Private identifiers are not allowed outside c
return super.expand(n) *Math.PI
Math.acosh(3) // 1.762747174039086 dog.printName()
}
Math.hypot(3, 4) // 5
Math.imul(Math.pow(2, 32) -1, Math.pow(2, 32) - Static private class
Static methods
# Promises
make the commitment Using Promises Using Promises in finally
Arrays
const scores = [22, 33] function greet({ name, greeting }) {
const [math = 50, sci = 50, arts = 50] = scores console.log(`${greeting}, ${name}!`)
const [first, last] = ['Nikola', 'Tesla']
}
Objects //Result:
//math === 22, sci === 33, arts === 50 greet({ name: 'Larry', greeting: 'Ahoy' })
let {title, author} = {
title: 'The Silkworm',
author: 'R. Galbraith'
}
Supports matching arrays and objects. See: A default value can be assigned when Destructuring of objects and arrays can also be
Destructuring destructuring an array or object done in function parameters
function greet({ name = 'Rauno' } = {}) { function printCoordinates({ left: x, top: y }) { for (let {title, artist} of songs) {
console.log(`Hi ${name}!`); console.log(`x: ${x}, y: ${y}`) ···
} } }
Object Deconstruction
The object spread operator allows you to build new The spread operator allows you to build new arrays
objects from other objects. See: Object Spread in the same way. See: Spread operator
# Functions
- Function parameters - Arrow function Parameter setting default value
function foo() {}
Default (default), rest, spread (extension). See: Like a function, but preserves this. See: Arrow foo.name // "foo"
function parameters functions
length property
See: Object Literals Enhanced See: Object Literals Enhanced See: Object Literals Enhanced
let event = 'click' const fatherJS = { age: 57, name: "Zhang San" }
let handlers = { Object.values(fatherJS)
[`on${event}`]: true //[57, "Zhang San"]
} Object.entries(fatherJS)
//Same as: handlers = { 'onclick': true } //[["age", 57], ["name", "Zhang San"]]
# Modules module
Imports import Exports export as keyword renaming
export {
import *as Helpers from 'helpers' const firstName = 'Michael';
v1 as streamV1, // export rename
//aka: const Helpers = require('···') const lastName = 'Jackson';
v2 as streamV2, // export rename
const year = 1958;
v2 as streamLatestVersion // export rename
export { firstName, lastName, year };
import { indentSpaces as indent } from 'helpers };
//aka: const indent = require('···').indentSpac
export *from "lib/math";
Dynamically load modules import() allows module paths to be dynamically generated - import.meta
button.addEventListener('click', event => { const main = document.querySelector('main') ES2020 Added a meta property import.meta to the
import('./dialogBox.js') import command, which returns the meta
.then(dialogBox => { import(`./modules/${someVariable}.js`) information of the current module
dialogBox. open(); .then(module => {
}) module.loadPageInto(main); new URL('data.txt', import.meta.url)
.catch(error => { })
/*Error handling */ .catch(err => {
}) main.textContent = err.message;
In the Node.js environment, import.meta.url
}); });
always returns a local path, that is, a string of the
file:URL protocol, such as file:///
ES2020 Proposal introduce import() function home/user/foo.js
- Import Assertions
static import
Dynamic Import
const json =
await import("./package.json", { assert: { type: "json" } })
# Generators
Generator function For..of + iterator Relationship with Iterator interface
# see also
Learn ES2015(babeljs.io)
ECMAScript 6 Features Overview (github.com)
中文版 #Notes
Kubernetes Cheatsheet TOML Cheatsheet PyTorch Cheatsheet Taskset Cheatsheet
Quick Reference Quick Reference Quick Reference Quick Reference