简体   繁体   中英

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. Plz provide some clear view.

ELIN (explain like I'm noob)

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. 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.

const x = 'World';
const answer = 42;
console.log`Hello, ${x} the answer is ${answer}`;

/*
[Arguments] {
  '0': [ 'Hello, ', ', the answer is ', '' ],
  '1': 'World',
  '2': 42
}
*/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM