问题
I've started Javascript 30. What bothers is like on given example code:
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
Why are the arguments put into the backticks? I understand how to use double and single quotes and when I can use both. However I don't understand how and when to use backticks.
回答1:
The backticks allow you to use the string interpolation syntax ${}.
回答2:
You can add expression in string with back-tick. This feature was add on ES6.
回答3:
Backticks are template literals. They allow for evaluation of variables and expressions inside of a string:
var a = 5, b = 10;
document.write(`${a} + ${b} = ${a + b}`);
来源:https://stackoverflow.com/questions/45516152/queryselector-method-arguments-are-put-into-backtick-why