簡體   English   中英

JavaScript 中的對象、Object 文字和模板文字有什么區別

[英]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.

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