提问者:小点点

ES6/ECMA6模板文字-不工作


我想尝试使用模板文本,但它不起作用:它显示的是文本变量名,而不是值。我使用的是Chrome v50.0.2(和jQuery)。

console.log('categoryName: ${this.categoryName}\ncategoryElements: ${this.categoryElements} ');
${this.categoryName} 
categoryElements: ${this.categoryElements} 

共3个答案

匿名用户

您需要使用反勾(也称为“重音”--如果使用QWERTY键盘,您可以在1键旁边找到它)--而不是单引号--来创建模板文字。

backtick在许多编程语言中都很常见,但对于JavaScript开发人员来说可能是新的。

categoryName="name";
categoryElements="element";
console.log(`categoryName: ${this.categoryName}\ncategoryElements: ${categoryElements} `) 
VM626:1 categoryName: name 
categoryElements: element

JavaScript中反勾字符(`)的用法

匿名用户

1.)将。jshitrc添加到与app.js和其他文件相同的文件夹级别

2.)将其放入新创建的文件{“ESVersion":6}

3.)切勿使用单引号“使用反勾号”

匿名用户

null

// Example
var person = {
  name: "Meera",
  hello: function(things) {
    console.log(`${this.name} Says hello ${things}`);
  }
}

// Calling function hello
person.hello("World");

//Meera Says hello World