JS: 2015 Features
What is ECMAScript 2015
- ECMAScript is the specification name for JavaScript.
- ECMAScript also known as ECMA-262, because it's number 262 of the spec organization called Ecma International.
- ECMAScript 2015 (aka ES6, ES2015, JS2015) is a major revision of the JavaScript language.
- ECMAScript 2015 is also known as ECMAScript 6 (ES6), because it is edition 6.
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 for declaring constants.
It's 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.
Rest Parameters
function can now specify to take any number of parameters, by syntax ...args
Destructuring Assignment
// destructuring assignment let [x, y, z] = [3, 4, 6];
Object Literal Expression, Computed Property Keys
new syntax for object literal to have computed property key by an expression.
obj = {[expression]:value}
Object Literal Expression, Method Definition Shorthand
new syntax shorthand for defining methods in object literal expression.
obj = {
method_name() {body},
etc
};
__proto__
Allow the use of property key __proto__
to get/set parent.
Template String
allows you to embed variable or expression in a string, or have newline char.
Spread Operator
The spread operator is 3 dots followed by a array, like this: ...arrayX
. What it does is effectively remove the bracket of the array.
for-of Loop
for-of loop lets you properly loop thru an array, or new datatypes such as set and map, or any iterable object. Iterable object is a new concept, created just for for-of loops.
Interface, Iterable, Iterator, Generator
Set Object
Set object lets you have a data structure of set. That is, a collection of unique values.
// create a set, add value to it var s = new Set(); s.add("6"); s.add("5"); s.add("6"); console.log(s); // Set { '5', '6' }
Map Object
Map object lets you have a data structure of key value pairs. Map object is similar to Python's dictionary or Ruby's hash. Similar to JavaScript Object, but now designed to be used as a data structure.
const m = new Map(); // add new item m.set(1, "n1"); m.set(2, "n2"); console.log(m); // 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 improved version of existing
methods.
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.flags
RegExp.prototype.sticky
RegExp.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
new array prototype properties
- Array.prototype.copyWithin
- Array.prototype.fill
- Array.prototype.find
- Array.prototype.findIndex
- Array.prototype.entries
- Array.prototype.keys
- Array.prototype.values
Array.prototype[Symbol.iterator]
(same as Array.prototype.values)Array.prototype [ Symbol.unscopables ]
New String Properties
static methods
string methods
String.prototype.localeCompare
String.prototype.normalize
String.prototype[Symbol.iterator]
New Object Methods
New Function Properties
function property "name"
New Number Properties
- Number.isFinite
- Number.isInteger
- Number.isNaN
- Number.isSafeInteger
- Number.parseFloat
- Number.parseInt
Number.EPSILON
Number.MIN_SAFE_INTEGER
Number.MAX_SAFE_INTEGER
new Math methods
Math [ Symbol.toStringTag ]
Math.sign
Math.fround
Math.trunc
Math.cbrt
Math.expm1
Math.log1p
Math.log10
Math.log2
Math.hypot
Math.sinh
Math.cosh
Math.tanh
Math.asinh
Math.acosh
Math.atanh
Math.clz32
Math.imul
html-like comments
block-level function declaration
// 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}
THE FOLLOWING IS WORK IN PROGRESS.
Syntax
- octal and binary literals
- new.target
Promise
- String static methods
- String.prototype methods
- RegExp.prototype properties
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
Annex b
- Object.prototype.__proto__
- hoisted block-level function declaration
- String.prototype HTML methods
- RegExp.prototype.compile
ArrayBuffer
ECMAScript 2015 §Structured Data#sec-arraybuffer-objects
dataview object
ECMAScript 2015 §Structured Data#sec-dataview-objects
tail call optimisation (recursion)
ECMAScript 2015 §ECMAScript Language: Functions and Classes#sec-tail-position-calls
WeakSet
ECMAScript 2015 §Keyed Collection#sec-weakset-objects
WeakMap
ECMAScript 2015 §Keyed Collection#sec-weakmap-constructor