简体   繁体   English

JavaScript 中的对象、Object 文字和模板文字有什么区别

[英]What is the difference between Objects, Object Literals and Template Literals in JavaScript

I've started just a while back with javascript but feel like a bit of confusion with drawing a clear line of distinction between these 3 topics in JS.我刚刚开始使用 javascript,但在 JS 中明确区分这三个主题时感觉有点混乱。 Plz provide some clear view.请提供一些清晰的观点。

ELIN (explain like I'm noob) ELIN(解释说我是菜鸟)

Objects -> Hashmaps (keys are strings, values are anything) Template Literals -> New form of passing multiline strings with inline computation.对象 -> 哈希图(键是字符串,值是任何东西)模板文字 -> 使用内联计算传递多行字符串的新形式。

examples:例子:

const x = 'world';
console.log(`Hello ${x}`) // will print Hello world

Template literals can be passed as complex information into functions, with a special syntax:模板字面量可以作为复杂信息传递给函数,具有特殊的语法:

myFunction`Hello, ${x}`;

This will execute myFunction with multiple arguments.这将使用多个 arguments 执行 myFunction。 The first one will be the non-computed strings, separated by those computed expressions as an array.第一个是非计算字符串,由这些计算表达式分隔为一个数组。 The rest of the arguments will the computed values. 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