Regular extension: http://es6.ruanyifeng.com/#docs/regex
Value extension: http://es6.ruanyifeng.com/#docs/number
Extension of function: http://es6.ruanyifeng.com/#docs/function
Array extension: http://es6.ruanyifeng.com/#docs/array
Extension of object: http://es6.ruanyifeng.com/#docs/object
1, the expansion of the regular
View Original
2, the expansion of the value
Number.isFinite()
Used to check whether a value is finite (finite), that is, not Infinity
.
Number.isNaN()
Used to check whether a value is NaN
.
Number.isInteger()
Used to determine whether a numeric value is an integer.
Number.isSafeInteger()
is used to determine whether an integer falls within this range. ES6 introduces Number.MAX_SAFE_INTEGER
and Number.MIN_SAFE_INTEGER
these two constants, which are used to denote the upper and lower bounds of this range.
Extension of the Math object
Math.trunc
method is used to remove the fractional part of a number and return the integer portion.
Math.sign
method is used to determine whether a number is positive, negative, or zero. For a non-numeric value, it is first converted to a numeric value.
Math.cbrt
method is used to calculate the cubic root of a number.
The logarithmic method, hyperbolic function method and exponential method are also extended.
3. Extension of functions
3.1. Parameter default value
3.2. Arrow function
ES6 allows you to define functions using the arrows ( =>
).
var f = v + v; // equivalent to var f = function (v) { return v;};
If the arrow function does not require arguments or requires more than one argument, a parenthesis is used to represent the parameter part.
var f = () = 5; // equivalent to return 5= (NUM1, num2) = Num1 + num2; // equivalent to var sum = function (NUM1, num2) { return num1 + num2;};
3.3, double colon operator
The arrow function can bind the this
object, greatly reducing the writing this
( call
, apply
, bind
) of the explicit bound object. However, the arrow function is not suitable for all occasions, so there is now a proposal to propose a function bind operator, which is used to replace call
, apply
bind
invoke.
The function-binding operator is a two-colon ()-side, ::
double-colon to the left of an object, and a function to the right. The operator automatically binds the left object, which is the context (that is, the this
object), to the function on the right.
Foo::bar; // equivalent to Bar.bind (foo); Foo::bar (... arguments); // equivalent to 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 equal to binding the method on top of the object.
var method = Obj::obj.foo; // equivalent to var method == :: console.log; // equivalent to var log = console.log.bind (console);
If the result of the operation of the double-colon operator, or an object, it is possible to use chained notation.
Import {map, takewhile, ForEach} from "Iterlib"== = x.strength >-console.log (x ));
4. Array extension: http://es6.ruanyifeng.com/#docs/array
5. Extension of object: http://es6.ruanyifeng.com/#docs/object
5.1. Attribute Shorthand
const foo = ' bar '; const baz =// {foo: "Bar"}// equals const baz = {Foo:foo};
Method attribute Shorthand
Let birth = ' 2000/01/01 '; Const person = { ' Zhang San ', // equal to Birth:birth Birth, // equivalent to Hello:function () ... This . Name); }};
The evaluator (setter) and accessor (getter) of a property are actually used in this notation.
Const cart = { 4, get Wheels () { returnthis. _wheels; } , set Wheels (value) { ifthis. _wheels) { throw New Error (' The value is too small! '); } this. _wheels = value; }}
If the value of a method is a Generator function, you need to precede it with an asterisk.
const obj = { * m () { ' Hello World '; }};
5.2. Traversal of attributes
ES6 there are altogether 5 ways to traverse the properties of an object.
(1) for...in
for...in
Iterates through the object's own and inherited enumerable properties (without the Symbol attribute).
(2) Object.keys (obj)
Object.keys
Returns an array that includes the key names of all enumerable properties (without the Symbol attribute) of the object itself (without inheritance).
(3) object.getownpropertynames (obj)
Object.getOwnPropertyNames
Returns an array that contains the key names of all the properties of the object itself (without the Symbol attribute, but including non-enumerable properties).
(4) object.getownpropertysymbols (obj)
Object.getOwnPropertySymbols
Returns an array that contains the key names of all the Symbol properties of the object itself.
(5) Reflect.ownkeys (obj)
Reflect.ownKeys
Returns an array that contains all the key names of the object itself, regardless of whether the key name is a Symbol or a string, or if it is enumerable.
The 5 methods above traverse the key names of the objects, all of which follow the same order rules for property traversal.
- All numeric keys are traversed first, sorted in ascending order of values.
- The next step is to iterate through all the string keys, sorted by join time.
- Finally, all Symbol keys are traversed, sorted in ascending order by join time.
Reflect.Ownkeys({[symbol (]: 0:010:< Span class= "token number" >02 :0 A:0 } //[' 2 ', ' Ten ', ' B ', ' A ', Symbol ()]
In the preceding code, the Reflect.ownKeys
method returns an array that contains all the properties of the Parameter object. The order of the attributes for this array is, first, the numeric attribute 2
and 10
, followed by the string attribute b
a
, and finally the Symbol attribute.
003-Regular expansion, numeric expansion, function expansion, array expansion, object expansion