1. Introduction The new features of
1.1 Babel TranscoderIt is a widely used ES6 transpiler. npm install --save-dev @babel/core Configuration file # Latest transcoding rules $ npm install --save-dev @babel/preset-env # react transcoding rules$ npm install --save-dev @babel/preset-react // The `presets` field sets the transcoding rules. The official website provides the following rule sets, which you can install as needed. { "presets": [ "@babel/env", "@babel/preset-react" ], "plugins": [] } 1.2 Polyfill By default, For example: $ npm install --save-dev core-js regenerator-runtime import 'core-js'; import 'regenerator-runtime/runtime'; 2. let and const 2.1 let In terms of scope, if(true){ let a = 1; var b = 2 } console.log(a) // ReferenceError: a is not defined console.log(b) // 2 Looking at the example below, we expect the output to be 1, because there is only one global variable i, so after var funcs = []; for (var i = 0; i < 5; i++) { funcs.push(function () { console.log(i); }); } funcs[1](); // 5 Fix: Use local storage for the i variable for each iteration and use a closure to close the scope. var funcs = []; for (var i = 0; i < 5; i++) { (function () { var local = i funcs.push(function () { console.log(local); }); } )() } funcs[1](); // 1 The same effect can be achieved by using 2.2 const if (true) { const PI = 3.141515926; PI = 66666 // TypeError: Assignment to constant variable. } console.log(PI) // ReferenceError: PI is not defined
const obj = {}; // Add an attribute to obj, which is successful obj.name = 'hello'; // Pointing obj to another object will result in an error obj = {}; // TypeError: "obj" is read-only 3. DeconstructionDeconstruction literally means decomposing the structure, that is, breaking the original structure. 3.1 Object DestructuringBasic usage: let { name, age } = { name: "hello", age: 12 }; console.log(name, age) // hello 12 Setting Default Values let { name = 'hi', age = 12 } = { name : 'hello' }; console.log(name, age) // hello 12 The let { name, ...remaining } = { name: "hello", age: 12, gender: '男' }; console.log(name, remaining) // hello {age: 12, gender: 'male'} 3.2 Array Destructuring The let [a, ...remaining] = [1, 2, 3, 4]; console.log(a, remaining) // 1 [2, 3, 4] Some members are ignored during array destructuring. let [a, , ...remaining] = [1, 2, 3, 4]; console.log(a, remaining) // 1 [3, 4] 3.3 Function parameter destructuringArray Parameters function add([x, y]){ return x + y; } add([1, 2]); // 3 Object Parameters function add({x, y} = { x: 0, y: 0 }) { return x + y; } add({x:1 ,y : 2}); 3.4 Common ScenariosSwap variables without using the third variable. let x = 1; let y = 2; [x, y] = [y, x]; Extracting JSON data let json = { code: 0, data: {name: 'hi'} }; let { code, data: user } = json; console.log(code, user); // 0 {name: 'hi'} Traversing the Map structure const map = new Map(); map.set('name', 'hello'); map.set('age', 12); for (let [key, value] of map) { console.log(key + " is " + value); } 4. Extension4.1 String ExtensionsTemplate string, this is very useful. Use backticks (`) to denote them. It can be used as a normal string, or it can be used to define multi-line strings or embed variables in strings. `User ${user.name} is login...`); 4.2 Function Extension Once the default values of the parameters are set, the parameters will form a separate function add(x, y = 1) { return x + y } Alternative to apply() // ES5 version Math.max.apply(null, [1, 3, 2]) // ES6 way of writing Math.max(...[1, 3, 2]) 4.3 Array ExtensionMerge Arrays // ES5 way of writing var list = [1,2] list = list.concat([3]) // ES6 way var list = [1,2] list = [...list, 3] New Array API Array.from(), Array.of(), find() and findIndex(), etc., see MDN https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array 4.4 Object ExtensionObject properties, method abbreviations data = [1,2] const resp = {data}; // attribute abbreviation, equivalent to {data: data} const obj = { add(x, y) { // Method abbreviation, equivalent to add: function(x, y){...} return x + y; } }; Extended attributes const point = {x: 1, y: 2} const pointD = {...point, z: 3} console.log(pointD) // {x: 1, y: 2, z: 3} // When there are repeated attributes, pay attention to the order. const point = {x: 1, y: 2} const pointD = {...point, x: 4, z: 3} console.log(pointD) // {x: 4, y: 2, z: 3} const point = {x: 1, y: 2} const pointD = {x: 4, z: 3, ...point} console.log(pointD) // {x: 1, z: 3, y: 2} The description object of the property Each property of an object has a const point = {x: 1, y: 2} Object.getOwnPropertyDescriptor(point, 'x') /** { configurable: true enumerable: true // indicates enumerable value: 1 writable: true // indicates writable} **/ Attribute traversal
const point = {x: 1, y: 2} for(let key in point){ console.log(key) } Some new methods of objects: Cloning an object function clone(origin) { return Object.assign({}, origin); } Merge Objects const merge = (target, ...sources) => Object.assign(target, ...sources); Specifying Default Values const DEFAULT_CONFIG = { debug: true, }; function process(options) { options = Object.assign({}, DEFAULT_CONFIG, options); console.log(options); // ... } 4.5 Operator ExtensionsExponential Operator 2 ** 10 // 1024 2 ** 3 ** 2 // 512 is equivalent to 2 ** (3 ** 2) let a=10; a **= 3; // equivalent to a = a * a * a Chaining operators const obj = {name: 'job', say(){console.log('hello')}} obj?.name // equals obj == null ? undefined : obj.name obj?.say() // equals obj == null ? undefined : obj.say() Null check operator In const obj = {name: ''} obj.name || 'hello' // 'hello' obj.name ?? 'hello' // '' 5. for…of Because const list = ['a', 'b', 'c'] for (let v in list){ console.log(v) // 0,1,2 } for (let v of list){ console.log(v) // a,b,c } 6. Summary This is the end of this article about the details of front-end JavaScript ES6. For more relevant You may also be interested in:
|
<<: HTML form tag tutorial (3): input tag
>>: How to deploy Rancher with Docker (no pitfalls)
Table of contents 1. Conditional access attribute...
CSS scroll bar style modification code .scroll::-...
1. Installation package preparation VMware-player...
Preface This article mainly introduces the analys...
Written in front There are two ways to upgrade My...
Table of contents Master-slave replication mechan...
1. Download the MySQL installation package First ...
I had been working on the project before the New ...
Table of contents mapState mapGetters mapMutation...
Table of contents Preliminary preparation Deploym...
Prerequisite: Percona 5.6 version, transaction is...
Environment: (docker, k8s cluster), continue with...
When it comes to bionic design, many people will t...
1. First check whether the system has mysql insta...
1. Environmental Preparation 1.MySQL installation...