[英]What is the `$` 's meaning in this code?
Here is partly code,I got confused about the $
's meaning. 这是部分代码,我对
$
的含义感到困惑。
function playSound(e) {
const audio = document.querySelector('audio[data-key="${e.keyCode}"]');
const key = document.querySelector('.key[data-key="${e.keyCode}"]');
...
}
I know the first code will return the first element <audio>
which has data-key="..."
.But I don't understand the $
's function and how to find it.So is there a document to explain it or anything else can help me understand it? 我知道第一个代码将返回具有
data-key="..."
的第一个元素<audio>
。但是我不理解$
的功能以及如何找到它。所以有文档来解释它还是有什么可以帮助我理解的?
I can assume you are taking the Javascript30 challenge. 我可以假设您正在接受Javascript30挑战。 As this code is from the first level there.
由于此代码是从第一级开始的。
Here ${}
is a special ES6 string interpolation. ${}
是特殊的ES6字符串插值。 To use this you have to use backticks. 要使用此功能,您必须使用反引号。 ( Here $ does NOT mean jQuery. )
(这里$并不表示jQuery。)
For example : The same thing above can be written as : 例如:上面的相同内容可以写成:
const key = document.querySelector('.key[data-key="' + e.keyCode + '"]');
Hence, instead of using the +
sign for the variables, it is much easier and better to use the ${}
for the string interpolation for the variables. 因此,不是使用
+
号作为变量,而是使用${}
进行变量的字符串插值要容易得多,更好。 Makes the code much easier to understand. 使代码更易于理解。 Please note that you need to use backticks to let the interpolation work.
请注意,您需要使用反引号使插值工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.