Several common JS syntactic sugars

Syntactic sugar

Syntactic sugar, also translated as sugar-coated grammar. Refers to a certain syntax added to a computer language. This syntax has no impact on the functionality of the language, but is more convenient for programmers to use.

Generally speaking, using syntax sugar can increase the readability of the program, thereby reducing the chance of program code errors.

"Syntax sugar" can bring us convenience. It is a convenient way of writing. The compiler will help us convert it; it can also improve the efficiency of development and coding without causing any loss in performance.

So, let’s learn some common [JS syntactic sugar]

1. Object literal
let sex1 = 'man', sex2 = 'woman'
let sex = {man,woman}  

2. Arrow function
let fun = function(params){}
//Can be abbreviated to the following arrow function will change this Point to
let fun= params =>{}
//When there are two or more parameters, as follows:
let fun= (params1,params2,,,)=>{}


3. Array deconstruction
let arr = ['a',' b','c'];
let {a,b} = arr
console.log(a) // a
//Array destructuring also allows you to skip values ​​you don't want to use, just leave the corresponding places blank, for example As follows
let {a,,c} = array
console.log(c) //c
 
4. Function default parameters
function getResponse(a,b=0) {   //Commonly used to set default values ​​when requesting data  } 5. Extended operation function test() {   return [...arguments] } test('a', 'b', 'c') // ['a','b','c'] //The expansion character can also combine arrays


 






 let all = ['1',...['2','3'],...['4','5'],'6'] // ["1", "2", " 3", "4", "5", "6"]
 
6. Template string
let id = 'rookie'
let blog = 'Blogger id is: ${a}' // Blogger id is:
1
7. Multi-line string
//Use backticks to implement multi-line string (although carriage return and line feed are the same string)
let poem = `A Pledge
    By heaven,
    I shall love you
    To the end of time!
    Till mountains crumble,
    Streams run dry,
    Thunder rumbles in winter,
    Snow falls in summer,
    And the earth mingles with the sky —
    Not till then will I cease to love you!`

8. Unpacking expression
const data = {       a: 'a',       b: 'b',       c: 'c' } let {a,c} = data





console.log(c); // c 
 
9.
Class helloJs in ES6 { // Constructor method   constructor(options = {}, data = []) {          this.name = 'rookie'         this.data = data         this. options = options     }  // Member method     getName() {          return this.name     } } 10. Modular development // Create a new util.js folder let formatTime = date=>{     .... } let endTime = date=> {     .... }



















module.exports = {    formatTime,    endTime, } //You can use import {name} from 'module'  //Then create a js file in the same directory and import util.js //import {endTime} from 'util' //Or All introduced //import util from 'util' The above is an introduction to common syntax sugar in JS









Guess you like

Origin blog.csdn.net/weixin_44821114/article/details/132956197