Usage of the backtick character (`) in JavaScript

后端 未结 9 2293
囚心锁ツ
囚心锁ツ 2020-11-22 00:51

In JavaScript, a backtick seems to work the same as a single quote. For instance, I can use a backtick to define a string like this:

var s         


        
9条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 01:17

    You can make a template of templates too, and reach private variable.

    var a= {e:10, gy:'sfdsad'}; //global object
    
    console.log(`e is ${a.e} and gy is ${a.gy}`); 
    //e is 10 and gy is sfdsad
    
    var b = "e is ${a.e} and gy is ${a.gy}" // template string
    console.log( `${b}` );
    //e is ${a.e} and gy is ${a.gy}
    
    console.log( eval(`\`${b}\``) ); // convert template string to template
    //e is 10 and gy is sfdsad
    
    backtick( b );   // use fonction's variable
    //e is 20 and gy is fghj
    
    function backtick( temp ) {
      var a= {e:20, gy:'fghj'}; // local object
      console.log( eval(`\`${temp}\``) );
    }
    

提交回复
热议问题