javascript中的`和'之间的区别(如果有) [英] Difference (if there is any) between ` and ' in javascript

查看:53
本文介绍了javascript中的`和'之间的区别(如果有)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近遇到了一些使用`和'的JS代码.我不知道每个撇号是否有不同的用法.有没有?

Recently ran into some JS code that uses ` and '. I can't figure out if there is a different use for each apostrophe. Is there any?

推荐答案

'" 表示 string

`表示模板字符串.模板字符串具有普通字符串不具备的一些功能.最重要的是,您可以获得插值:

` denotes a template string. Template strings have some abilities that normal strings do not. Most importantly, you get interpolation:

var value = 123;
console.log('test ${value}') //=> test ${value}
console.log(`test ${value}`) //=> test 123

多行字符串:

console.log('test
test')
// Syntax error

console.log(`test
test`)
// test
// test

他们还有其他一些技巧,更多关于模板字符串的信息:

They have some other tricks too, more on template strings here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings

请注意,目前并非所有的javascript引擎都支持它们.这就是为什么模板字符串经常与诸如 Babel.这样的编译器一起使用的原因.解释器.

Note they are not supported in all javascript engines at the moment. Which is why template strings are often used with a transpiler like Babel. which converts the code to something that will work in any JS interpreter.

这篇关于javascript中的`和'之间的区别(如果有)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆