Regular extension: http://es6.ruanyifeng.com/#docs/regex
Extensions for numbers: http://es6.ruanyifeng.com/#docs/number
Extension of function: http://es6.ruanyifeng.com/#docs/function
Array extension: http://es6.ruanyifeng.com/#docs/array
Object extension: http://es6.ruanyifeng.com/#docs/object
1. Regular extension
View original text
2. Numerical expansion
Number.isFinite()
Used to check if a value is finite, i.e. not Infinity
.
Number.isNaN()
Used to check if a value is NaN
.
Number.isInteger()
Used to determine whether a value is an integer.
Number.isSafeInteger()
It is used to determine whether an integer falls within this range. ES6 introduced the constants Number.MAX_SAFE_INTEGER
and Number.MIN_SAFE_INTEGER
to represent the upper and lower bounds of this range.
Extensions to Math Objects
Math.trunc
The method is used to remove the fractional part of a number and return the integer part.
Math.sign
The method is used to determine whether a number is positive, negative, or zero. For non-numeric values, it is converted to a numeric value first.
Math.cbrt
method is used to calculate the cube root of a number.
It also extends the logarithmic method, the hyperbolic function method, the exponential method
3. Function extension
3.1, parameter default value
3.2, arrow function
ES6 allows =>
functions to be defined using "arrows" ( ).
var f = v => v; // same as var f = function (v) { return v; };
If the arrow function requires no parameters or requires more than one parameter, use a parenthesis to represent the parameter part.
var f = () => 5 ; // same as var f = function () { return 5 }; var sum = (num1, num2) => num1 + num2; // same as var sum = function(num1, num2) { return num1 + num2; };
3.3, double colon operator
Arrow functions can bind objects, greatly reducing the writing method this
of explicitly binding objects ( , , ). However, arrow functions are not suitable for all occasions, so there is now a proposal for a "function bind" operator to replace , , and call.this
call
apply
bind
call
apply
bind
The function binding operator is two colons ( ::
) side by side, with an object on the left of the double colon and a function on the right. This operator automatically this
binds the object on the left, as the context (ie, the object), to the function on the right.
foo::bar; // same as bar.bind(foo); foo::bar(...arguments); // same as bar.apply(foo, arguments); const hasOwnProperty = Object.prototype.hasOwnProperty; function hasOwn(obj, key) { return obj::hasOwnProperty(key); }
If the left side of the double colon is empty and the right side is a method of an object, it is equivalent to binding the method to the object.
var method = obj::obj.foo; // same as var method = ::obj.foo; let log = ::console.log; // same as var log = console.log.bind(console);
If the operation result of the double colon operator is still an object, the chained writing method can be used.
import { map, takeWhile, forEach } from "iterlib"; getPlayers() ::map(x => x.character()) ::takeWhile(x => x.strength > 100) ::forEach(x => console.log(x));
4. Array extension: http://es6.ruanyifeng.com/#docs/array
5. Object extension: http://es6.ruanyifeng.com/#docs/object
5.1, attribute shorthand
const foo = 'bar'; const baz = {foo}; baz // {foo: "bar"} // same as const baz = {foo: foo};
Method property shorthand
let birth = '2000/01/01'; const Person = { name: 'Zhang San' , // equivalent to birth: birth birth, // same as hello: function ()... hello() { console.log('My name is', this .name); } };
The attribute's assigner (setter) and valuer (getter) are actually written in this way.
const cart = { _wheels: 4, get wheels () { return this._wheels; }, set wheels (value) { if (value < this ._wheels) { throw new Error('The value is too small!' ); } this._wheels = value; } }
If the value of a method is a Generator function, it needs to be preceded by an asterisk.
const obj = { * m() { yield 'hello world'; } };
5.2. Traversal of properties
ES6 has a total of 5 ways to traverse the properties of an object.
(1)for...in
for...in
Loop through the object's own and inherited enumerable properties (excluding Symbol properties).
(2)Object.keys(obj)
Object.keys
Returns an array containing the keys of all enumerable properties (excluding Symbol properties) of the object's own (excluding inherited) properties.
(3)Object.getOwnPropertyNames(obj)
Object.getOwnPropertyNames
Returns an array containing the key names of all properties of the object itself (excluding Symbol properties, but including non-enumerable properties).
(4)Object.getOwnPropertySymbols(obj)
Object.getOwnPropertySymbols
Returns an array containing the key names of all the Symbol properties of the object itself.
(5)Reflect.ownKeys(obj)
Reflect.ownKeys
Returns an array containing all the key names of the object itself, whether the key name is a Symbol or a string, and whether it is enumerable or not.
The above five methods traverse the key names of the object, and all follow the same property traversal order rules.
- First iterate over all numeric keys, in ascending numerical order.
- Next, iterate over all string keys and sort them in ascending order by joining time.
- Finally, all Symbol keys are traversed and sorted in ascending order according to the joining time.
Reflect.ownKeys({ [Symbol()]:0, b:0, 10:0, 2:0, a:0 }) // ['2', '10', 'b', 'a', Symbol()]
In the above code, the Reflect.ownKeys
method returns an array containing all the properties of the parameter object. The property order of this array is such that the numeric property sums first 2
, 10
the string property sums second b
, and a
the Symbol properties last.