[英]What is more Canonical javascript, console.log
I have recently started using typescript and reviewing some of my favorite repos, I have seen people use back ticks for template string in console.log
.我最近开始使用 typescript 并查看了一些我最喜欢的存储库,我看到人们在
console.log
中使用反引号作为模板字符串。 This seems odd to me, but I am pretty inexperienced overall with canonical javascript.这对我来说似乎很奇怪,但我对规范的 javascript 总体上非常缺乏经验。 To me,
console.log
already provides the mechanism for formatting by putting in spaces betwixt comments.对我来说,
console.log
已经通过在注释之间添加空格来提供格式化机制。 Why would you use a template string?为什么要使用模板字符串? Is the performance better?
性能更好吗?
Example:例子:
// set list of items
function printMyItems(item1, item2) {
// What I have been doing
console.log(item1, item2);
// What I have seen
console.log(`${item1} ${item2}`);
}
// Also variadic arguments seem to work better with the former approach.
function printMyItems(...args) {
// What I have been doing
console.log(...args);
// Should I do this instead?
console.log(`${...args}`);
}
Thank you for your time and effort reading this post.感谢您花时间和精力阅读这篇文章。 I hope I have made myself clear.
我希望我已经说清楚了。
There is actually a distinct difference between the 2 implementations when it comes to objects:当涉及到对象时,这两种实现之间实际上存在明显差异:
var test = {a: 1}; console.log(`${test}`); test.a = 99; console.log(`${test}`); var test2 = {a: 1}; console.log(test2); test2.a = 99; console.log(test2);
var test1 = {a: 1}; var test2 = {b: 99}; console.log(`${test1}` == `${test2}`) console.log(test1 == test2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.