[英]What is the difference between Objects, Object Literals and Template Literals in JavaScript
我剛剛開始使用 javascript,但在 JS 中明確區分這三個主題時感覺有點混亂。 請提供一些清晰的觀點。
ELIN(解釋說我是菜鳥)
對象 -> 哈希圖(鍵是字符串,值是任何東西)模板文字 -> 使用內聯計算傳遞多行字符串的新形式。
例子:
const x = 'world';
console.log(`Hello ${x}`) // will print Hello world
模板字面量可以作為復雜信息傳遞給函數,具有特殊的語法:
myFunction`Hello, ${x}`;
這將使用多個 arguments 執行 myFunction。 第一個是非計算字符串,由這些計算表達式分隔為一個數組。 arguments 的 rest 將計算值。
const x = 'World';
const answer = 42;
console.log`Hello, ${x} the answer is ${answer}`;
/*
[Arguments] {
'0': [ 'Hello, ', ', the answer is ', '' ],
'1': 'World',
'2': 42
}
*/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.