我从一些 JavaScript/Node.js
项目中阅读了一些代码(似乎是 ES6),但我对语法感到困惑:
var c = `
export const imports = () => {
const mods = []
${files.map((v) => `
['1234', 333]
`)}
return Promise.all(mods)
}
export default imports
`
这会给我
> c
'\n export const imports = () => {\n const mods = []\n \n[\'1234\', 333]\n,\n[\'1234\', 333]\n\n return Promise.all(mods)\n }\n export default imports\n '
如果在 Node.js 中运行。
我猜这是一个多行字符串,我试过了:
var s = `
some multiline
indented string`
['1234', 333]
`another multiline
indented string
`
但是我得到了三个子句:
> var s = `
... some multiline
... indented string`
undefined
> ['1234', 333]
[ '1234', 333 ]
> `another multiline
... indented string
... `
'another multiline\n indented string\n '
谁能帮我解决语法问题?它使用哪个 ECMAScript 规范?希望有人能给我具体规范 anchor 的链接。
最佳答案
这是 template literals 的示例.
请注意 ${ ... } 中的部分是字符串插值——在这种情况下令人困惑的是插值本身(下面的代码)包含一个文字字符串。
files.map((v) => `
['1234', 333]
`)
关于javascript - 一个令人困惑的 JavaScript ES6 字符串模板语法示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56069996/