JS: ECMAScript 2015
Over 10 major features are added to the language, effectively making JavaScript a new language.
Here is a quick overview of new features.
Let
let is like var , but with a clean block-level scope.
Const
const is similar to let, except you can't change its value once set.
Arrow function
you can write function like this
((x, y) => { return x + y; });
but no this-binding , and no arguments Object, no Function Name Hoisting .
Function argument default values
function parameter can specify a default value.
// function with default parameter values function f(x = 4, y = 2) { return [x, y]; } console.log(f()); // [ 4, 2 ]
Rest parameters
function can now specify to take any number of parameters, by syntax ...args
// 2 parameters plus a rest param function ff(a, b, ...c) { return c; } console.log(ff(1, 2, 3, 4)); // [ 3, 4 ]
Assignment pattern
// assignment pattern let [x, y, z] = [3, 4, 6];
Object literal expression, extensions
Computed Property Keys
new syntax for object literal to have computed property key by an expression.
// Object Literal Expression, Computed Property Keys const expr = "cat"; const xobj = { [expr]: 4 }; console.log(xobj); // { cat: 4 }
Method Definition Shorthand
new syntax shorthand for defining methods in object literal expression.
// object literal expression method definition shorthand const jj = { xmethod(x) { return x + 1; }, gg: 4, }; console.log(jj.xmethod(2)); // 3
Key without value Shorthand
{ a }
means
{ a: a }
if a is not defined, it's an error.
// key without value shorthand for object literal expression const dog = 3; const cat = 4; const jj = { dog, cat }; console.log(jj); // { dog: 3, cat: 4 }
Template string
allows you to embed variable or expression in a string, or have newline char.
// template string example console.log(`2 + 3 is ${2 + 3}`); // 2 + 3 is 5
Interface, iterable, iterator, generator
Iterable is new. It is a like an delayed array, items are generated at time it is used.
array is made iterable.
iterator is created to support iterable.
for-of loop and spread operator works with iterables.
these are created to support iterable.
- JS: Interface
- Iterator prototype
- JS: Generator
Generator Function is created to support creating iterable.
Spread operator
The spread operator is 3 dots:
...xArray
What it does is insert the array items (into a array, or as function args). The operand can be any iterable.
For-of loop
loop thru an iterable.
Set object
Set object is data structure of set. That is, a collection of unique values.
Map object
Map object is a data structure of dictionary. Similar to Data Object, but now designed to be used as a data structure, have length, keys can be any type (not just string), etc.
const xx = new Map(); // add new item xx.set(1, "n1"); xx.set(2, "n2"); console.log(xx); // Map { 1 => "n1", 2 => "n2" }
Reflect
The Reflect object is used as namespace to host a group of static functions.
Reflect methods typically duplicate operator behavior in function form, or provides a more strict or better version of existing methods.
ReflectReflect.getReflect.getReflect.setReflect.hasReflect.definePropertyReflect.deletePropertyReflect.getOwnPropertyDescriptorReflect.ownKeysReflect.getPrototypeOfReflect.setPrototypeOfReflect.isExtensibleReflect.preventExtensionsReflect.applyReflect.construct
Class
class is a new keyword that lets you conveniently define object and its prototype.
New keywords are:
class,
constructor,
static,
extends,
super.
Regex
New regex flag "u". For unicode. Treat string as sequence of unicode characters.
New regex flag "y". For “sticky”. This means next regex match call will start with lastIndex.
New value properties.
RegExp.prototype.flagsRegExp.prototype.stickyRegExp.prototype.unicode
New function properties.
RegExp.prototype [ Symbol.match ](str)RegExp.prototype [ Symbol.replace ](str, replace)RegExp.prototype [ Symbol.search ](str)RegExp.prototype [ Symbol.split ](str, limit)
New array properties
new array constructor properties
- convert array-like and iterable to array.
- JS: Array.from
- function form of array literal expression
- JS: Array.of
new array prototype properties
- JS: Array.prototype.copyWithin
- JS: Array.prototype.fill
- JS: Array.prototype.find
- JS: Array.prototype.findIndex
- JS: Array.prototype.entries
- JS: Array.prototype.keys
- JS: Array.prototype.values
Array.prototype[Symbol.iterator](same as Array.prototype.values)Array.prototype[Symbol.unscopables]
New string properties
static methods
String.fromCodePoint(...codePoints)— builds a string from Unicode code points (including those above U+FFFF)String.raw(template, ...substitutions)— tag function that returns the raw template string (no escape processing)
string methods
String.prototype.localeCompareString.prototype.normalizeString.prototype[Symbol.iterator]
New object methods
New function properties
function property "name"
New number properties
- JS: Number.isFinite
- JS: Number.isInteger
- JS: Number.isNaN
- JS: Number.isSafeInteger
- JS: Number.parseFloat (String to Number)
- JS: Number.parseInt (String to Number)
Number.EPSILONNumber.MIN_SAFE_INTEGERNumber.MAX_SAFE_INTEGER
New math methods
Math[Symbol.toStringTag]
Math.signMath.truncMath.cbrt
Math.expm1Math.log1pMath.log10Math.log2
Math.hypot
Math.sinhMath.coshMath.tanhMath.asinhMath.acoshMath.atanh
Math.froundMath.clz32Math.imul
Html-like comments
formally recognize html comment.
Block-level function declaration
now allow function declaration inside a block.
// es2015 block-level function declaration function f() { function g(x) { return x + 1; } return g(1); } console.log(f()); // 2
Module, keyword “import”, “export”
Unicode escape syntax
Now you can escape unicode by
\u{4_to_6_hex_digits}
Octal and binary literals
// octal notation console.log( 0o10 ) // 8 // binary console.log( 0b10 ) // 2
New.target
new.target is a special meta-property in JavaScript that tells you whether a function (or class constructor) was invoked using the new keyword.
function ff() { if (new.target) console.log("called with new.\n", "new.target value is: ", new.target); else console.log("called as function.\n", "new.target value is:", new.target); } new ff(); // called with new. // new.target value is: [Function: ff] ff(); // called as function. // new.target value is: undefined
Promise
PromisePromise.prototypePromise.allPromise.racePromise.rejectPromise.resolve
Promise.prototype.catch ( onRejected )Promise.prototype.constructorPromise.prototype.then ( onFulfilled , onRejected )Promise.prototype [ @@toStringTag ]
Subclassing
- Array is subclassable
- RegExp is subclassable
- Function is subclassable
- Promise is subclassable
- miscellaneous subclassables
Misc
- prototype of bound functions
- Object static methods accept primitives
- own property order
- miscellaneous
__proto__
document the property key __proto__ to get/set parent.
It was from Firefox.
Annex b
- Object.prototype.__proto__
- hoisted block-level function declaration
- String.prototype HTML methods
- RegExp.prototype.compile
Arraybuffer, dataview, typed array
ArrayBufferDataView
Int8ArrayInt16ArrayInt32ArrayUint8ArrayUint16ArrayUint32ArrayUint8ClampedArrayFloat32ArrayFloat64Array
Weakmap, weakset
Proxy
it allows you to create a new object, such that you can intercept any operation with the object.