Extensions to (Regular, Numeric, Array, Object)

Keywords: ECMAScript

Regular, Numeric, Array, Object

1. Regular Expansion

  • Provided   u   Modifier for regular recognition of two-character special words;

 

1 let result=/Pyramid{2}/u.test('Pyramid');
2 console.log(result);

 

  • Provided   y   Modifier, which continues to match down after one match;
1 let text='xxx_xx_xx_',
2     patt=/x+_/y;
3 console.log(patt.exec(text));
4 console.log(patt.exec(text));
5 console.log(patt.exec(text));
  •   y   Modifier   stikcy   Property to detect the existence of y   Modifier
1 console.log(patt.sticky);
  • flags   Property that returns the name of the regular use modifier
1 console.log(patt.flags);
  •  .    Represents a match of all, except for terminators such as carriage return\nwrapnand so on, using the s modifier to match
1 let text='x\nyz',
2     patt=/x.+yz/s;
3 console.log(patt.test(text));
  • Modifier substitution
1 // ES6 Modifier substitution is supported, and previous writings will directly error;
2 let regex=new RegExp(/xyz/iu,'g')
3 console.log(regex.flags);

2. Extension of Values

  • ES6 explicitly prefixes binary, octal, and hexadecimal with 0b, 0o, and 0x, respectively;
1  console.log(number(value('0b00')));
2  console.log(number(value('0o00')));
3  console.log(number(value('0x00')));
  • ES6 provides Number.isFinitel(), Number.isNaN() to judge infinity and NaN;
1 Nonnumeric is false Value is true
2  console.log(Number.isFinite(100));
3  NaN by true,The rest are false
4  console.log(Number.isNaN(100));
  • ES6 provides Number.parseInt(), Number.parseFloat() conversion integers and floating-point types;
1  console.log(Number.parseInt('5.555a'));
2  console.log(Number.parseFloat('5.555b'));
  • ES6 provides Number.isInteger() to determine if the parameter is an integer type;
1  console.log(Number.isInteger('10'));
  • ES6 provides a constant with a very small value to determine if the correct result is obtained
1  console.log((0.1+0.2+0.3).toFixed(20));
2  console.log(Number.EPSILON);
3  console.log(Number.EPSILON.toFixed(30));
4  console.log((0.1+0.2+0.3)<Number.EPSILON);
  • ES6+ has a new exponential operator** and can perform assignment operations.
1  console.log(2**3);  //16  Multiply Three Two
2  let num=2;
3  num **=5;
4  console.log(num);   //32  Multiply five two

3. Extension of Arrays

  1. Operator Extensions

  • ES6 provides (...) three points to convert an array into comma-splits for processing
1 function add(x,y){
2     return x+y;
3 }
4 console.log(add(...[10,20]));
  • Maximum
1 console.log(Math.max(...[1,2,3]));
  • Merge Array
1 console.log([...[1,2],...[3,4]]);

        2. Extension of methods

  • ES6 provides the Array.of() method, whose primary purpose is to make up for Array()
1 ES6 Declare Array
2 let items=Array.of(3,2,1);
3 console.log(items);
  • ES6 provides the Array.from() method to convert an array-like object or traversal into a real array
 1 let obj={
 2     0:'name',
 3     1:'age',
 4     2:'gender',
 5     length:3
 6 };
 7 console.log(Array.from(obj));
 8 PS: Converting objects to arrays is a strict requirement:
 9     (1).key Must be a numeric or string number; 
10     (2).length Set the length, and key Within range;
11 (1).DOM Of NodeList aggregate;
12 let nl=document.querySelectorAll('p');
  • ES6 provides find() and findIndex() methods to find the first matching value in an array
1 let items=[10,20,30,40,50];
2 console.log(items.find(value=>value>19));
  • ES6 provides a fill() method to populate element values in an overridden array
1 let items=[10,20,30,40,50];
2 console.log(items.fill('a',2,3)); //Index 2 Begins Index 3 Ends
  • ES6 provides the copyWithin method, copies the values from within the array, and pastes the specified location
1 let items=[10,20,30,40,50];
2 //Copy values from index 0 
3 //Then paste the values from index 2 
4 //Parameter 3 Setting End Paste Index Value
5 console.log(items.copyWithin(2,0));

4. Short Description of Objects

  • Abbreviation of Attribute Initial Value in Object Literal Quantity
1 function fn(name,age){
2     return{
3         name,age
4     }
5 }
6 console.log(fn('Mr.Lee',100));
  • Short form of method in object literal quantity
1 let obj={
2     fn(){
3         return 'fn'
4     }
5 };
6 console.log(obj.fn());
  • Allow assembly of attribute names using expressions in object literals
 1 Assembly Combination Properties
 2 let obj={
 3     // Assembly Combination Properties
 4     ['user'+'Name']:'Mr.Lee',
 5     //With spaces
 6     ['user' + ' Age'] : 100,
 7     //String Properties
 8     'user Gender' : 'male'
 9 };
10 console.log(obj.userName);
11 console.log(obj['userName']);
12 console.log(obj['user Age']);
13 console.log(obj['user Gender']);
  • ES6 provides the use of calculable (dynamic) attribute names in object literals
 1 //Through variables myName Dynamic Change obj Property Name 
 2 //The problem is that when the value of a variable changes, the property name becomes invalid 
 3 let myName = 'name'; 
 4 let obj = { 
 5     name : 'Mr.Lee'
 6 };
 7 console.log(obj[myName]); 
 8 //Use[myName]Computable Property Name 
 9 //Real dynamic computing implemented 
10 let myName = 'name'; 
11 let obj = {
12      [myName] : 'Mr.Lee' 
13 };
14 console.log(obj[myName]);
  • ES6 can also use a collage name on the object literal method
1 let obj = {
2     ['f' + 'n']() {
3         return 'fn';
4     }
5 };
6 console.log(obj.fn());

5. New Methods for Objects

  • ES6 provides the Object.is() method to address some of the equivalents in "==="
1 console.log(Object.is(100, '100'));   //false,Must be equal
2 console.log(Object.is({}, {}));  //false,Point to different
3 console.log(Object.is(+0, -0));   //false
4 console.log(Object.is(NaN, NaN));   //true
5 console.log(NaN === NaN);   //false
6 console.log(+0 === -0);   //true
  • ES6 provides the Object.assign() method to merge specified objects into the target object
1 /...Pass-through, merge all objects, return to the target object of parameter one
2 console.log(Object.assign(obj1, obj2, obj3));
3 //The first object gets the merged result;
4 console.log(obj1);
5 
6 // PS:  (1).If the attributes are the same, the content of the source object after overrides the previous attribute values. 
7 //      (2).If non-object content is passed directly, it is converted to an object. 
8 //      (3).If the incoming is undefined and null Errors will be made;
  • ES6 provides Object.getPrototypeOf() and Object.setPrototypeof() methods
 1 let obj = {
 2     fn() {
 3         return 'fn';
 4     }
 5 };
 6 let obj2 = {
 7     fn() {
 8         return 'fn2';
 9     }
10 };
11 //with obj Object as prototype 
12 let f = Object.create(obj)
13 //Detection is obj Whether f Prototype object 
14 console.log(Object.getPrototypeOf(f) === obj);
15 //Output prototype object's fn console.log(f.fn()); 
16 //Set up f The prototype object is obj2 
17 Object.setPrototypeOf(f, obj2);
18 console.log(Object.getPrototypeOf(f) === obj2);
19 console.log(f.fn());
  • ES6 provides the super keyword for inheritance of methods in the prototype
 1 let obj = {
 2     fn() {
 3         return 'fn';
 4     }
 5 };
 6 let f = {
 7     fn() {
 8         return super.fn() + ', extend!';
 9     }
10 };
11 //Set up obj yes f Prototype 
12 Object.setPrototypeOf(f, obj);
13 console.log(f.fn());
14 //Can be set to f As prototype 
15 let h = Object.create(f);
16 console.log(h.fn());

 

Posted by whmeeske on Fri, 26 Nov 2021 17:22:24 -0800