簡體   English   中英

還有什么Canonical javascript,console.log

[英]What is more Canonical javascript, console.log

我最近開始使用 typescript 並查看了一些我最喜歡的存儲庫,我看到人們在console.log中使用反引號作為模板字符串。 這對我來說似乎很奇怪,但我對規范的 javascript 總體上非常缺乏經驗。 對我來說, console.log已經通過在注釋之間添加空格來提供格式化機制。 為什么要使用模板字符串? 性能更好嗎?

例子:

// 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}`);
}

感謝您花時間和精力閱讀這篇文章。 我希望我已經說清楚了。

當涉及到對象時,這兩種實現之間實際上存在明顯差異:

 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM