Use template string

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )
  1. Take variables

       var name = "liyang";
       var age = 23;
       console.log(`I am ${name},I ${age} years old`)
    

     如果想用到反引号,用斜杠转义
       var name = "liyang";
       var age = 23;
       console.log(`I \`\`am ${name},I ${age} years old`)
    

  2. Display multi-line strings

       $('#root').html(`
           <ul>
               <li>一</li>
               <li>二</li>
               <li>三</li>
           </ul>
       `)
      
    

    <Ul> tag will be in front of a line break. If you want the first row and the end row of line feeds, spaces, etc. is removed, using methods trim.

           $('#root').html(`
           <ul>
               <li>一</li>
               <li>二</li>
               <li>三</li>
           </ul>
       `.trim())
    
  3. Embedding a plurality of variables are used

    var  x= 1, y = 2;
    console.log(`${x} + ${y} = ${x + y}`)
    

    var x = 1, y = 2;
    console.log ( ${x} + ${y * 2} = ${x + y * 2})

    var obj = {x: 1, 2};
    console.log ( ${obj.x + obj.y});

  4. call function

    function func(){
      return 'Hello';
    }
    console.log(`${func()} World`);
    

Guess you like

Origin blog.csdn.net/qq_43258252/article/details/94745722
Recommended