美元符号后跟模板字符串中的方括号

美元符号后跟模板字符串中的方括号

本文介绍了美元符号后跟模板字符串中的方括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搞砸一些ES6代码,发现这个

I was messing around with some ES6-code and came across this

let vendors = ['ms', 'moz', 'webkit', 'o'];
let root = window || global;
let performance = window.performance || {};
if (!performance.now) {
  vendors.some(function(vendor) {
    performance.now = performance[`$[vendor}Now`];
    ...

我可以猜测下面的代码片段,但是什么样的库/语法呢?这不是我以前见过的,它不是纯ES6,对吗?

I can guess what the code-piece below does, but what kind of library/syntax is it? It's not something I have ever seen before, and it's not pure ES6, right?

`$[vendor}Now`


推荐答案

似乎这是一个语法错误应该是:

It seems that this is a syntax error. The correct thing should be:

`${vendor}Now`

这是在这里提到的美元表达式:

This is the dollar expression as it is mentioning here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/template_strings

模板字符串中的方括号是一个错误。

The square bracket in a template string is a mistake.

更多具体如果你有:

var expression = 'test';

console.log(`string text ${expression} string text`); //Correct syntax

上述代码将导出:string text test string text

The above code will export: "string text test string text"

但是下面的代码有一个开方括号和一个关闭

But the below code with one opening square bracket and one closing curly bracket

var expression = 'test';

console.log(`string text $[expression} string text`); //Wrong syntax

只会导出:string text $ [expression} string text

Will just export: "string text $[expression} string text"

这篇关于美元符号后跟模板字符串中的方括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:54