使用ES6模板文字语法没有模板表达式有什么缺点吗? [英] Is there a downside to using ES6 template literals syntax without a templated expression?

查看:216
本文介绍了使用ES6模板文字语法没有模板表达式有什么缺点吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个原因(性能或其他)不对JavaScript源文件中的所有字符串使用反引号模板文字语法?如果是,那么什么?

Is there a reason (performance or other) not to use backtick template literal syntax for all strings in a javascript source file? If so, what?

我应该更喜欢这个:

var str1 = 'this is a string';

这个?

var str2 = `this is another string`;


推荐答案

不使用它们的最重要的原因是ES6在所有环境中都不支持。

The most significant reason not to use them is that ES6 is not supported in all environments.

当然这可能不会影响你,但仍然: YAGNI 。不要使用模板文字,除非您需要插值,多行文字或未转义的引号和撇号。 什么时候在JavaScript中使用双引号或单引号?的大部分论据也会继承。一如往常,保持你的代码基础一致,只使用一个字符串文字风格,你不需要一个特殊的。

Of course that might not affect you at, but still: YAGNI. Don't use template literals unless you need interpolation, multiline literals, or unescaped quotes and apostrophes. Much of the arguments from When to use double or single quotes in JavaScript? carry over as well. As always, keep your code base consistent and use only one string literal style where you don't need a special one.

这篇关于使用ES6模板文字语法没有模板表达式有什么缺点吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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