Javascript ES6 / ECMA6 模板文字 - 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37245679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:04:16  来源:igfitidea点击:

ES6 / ECMA6 template literals - not working

javascriptjqueryecmascript-6

提问by Ron I

I wanted to try using template literalsand it's not working: it's displaying the literal variable names, instead of the values. I am using Chrome v50.0.2 (and jQuery).

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

Example:

例子:

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

Output:

输出:

${this.categoryName} 
categoryElements: ${this.categoryElements} 

回答by Tim Grant

JavaScript template literalsrequire backticks, not straight quotation marks.

JavaScript模板文字需要反引号,而不是直引号。

You need to use backticks (otherwise known as "grave accents" - which you'll find next to the 1 key) - rather than single quotes - to create a template literal.

您需要使用反引号(也称为“重音符” - 您会在 1 键旁边找到它) - 而不是单引号 - 来创建模板文字。

Backticks are common in many programming languages but may be new to JavaScript developers.

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

Example:

示例

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

Output:

输出

VM626:1 categoryName: name 
categoryElements: element

See: What is the usage of the backtick symbol (`) in JavaScript?

请参阅: JavaScript 中反引号 (`) 的用法是什么?

回答by Abdelaziz El-emary

you need to use `` instead of single ' ' or double quotes " " . these quotes will be found in the button on the left of number 1.

您需要使用 `` 而不是单 ' ' 或双引号 " "。这些报价将在数字 1 左侧的按钮中找到。

Happy coding !!

快乐编码!!

回答by Aljohn Yamaro

1.) add .jshitrc same folder level with your app.js and other files

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

2.) put this inside the newly created file { "esversion": 6 }

2.) 把它放在新创建的文件中 { "esversion": 6 }

3.) never use single quote ' use backticks `

3.) 永远不要使用单引号 ' 使用反引号 `

回答by sunilsingh

// 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