节点文档中的这个 `...${...}...` 代码是什么意思? [英] What does this `…${…}…` code in the node docs mean?

查看:20
本文介绍了节点文档中的这个 `...${...}...` 代码是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算一步一步地学习 Express 库和 Node.js.首先我看的是 Node reqiure(moduleName) 函数的细节.

我查看了文档,发现了一些奇怪的代码示例文档:

const circle = require('./circle.js');console.log(`半径为4的圆的面积是${circle.area(4)}`);

更具体地说是 ${circle.area(4)} 位.

据我所知,JavaScript 中的 $ 就像任何其他变量一样.当我们在客户端 Web 开发中使用它时,它被用作文档功能的委托(我认为).使用节点时分配给什么?

除此之外,这个语法是什么意思?${circle.area(4)}

如果 $ 只是对某个函数的引用 someFunction(),那岂不是等价于这个 someFunction(){cirle.area(4)}.我不知道这怎么可能是有效的语法.

另外,他们为什么不直接调用 circle.area() 函数呢?

解决方案

这个:

`半径为4的圆的面积是${circle.area(4)}`

ES2015 模板字符串的示例.

它将circle.area(4) 表示的任何内容直接插入到字符串中.如果您对这个或其他 ES2015 功能感到好奇,我建议您查看 Babel 并在 REPL 中玩玩.

这是一个非常简单的例子 让你开始.

你可以看到这个 ES2015 代码:

const foo = 'some text';console.log(`${foo} 是内插的.`);

被转译为 ES5 等价物 - 一个简单的 + 连接:

var foo = 'some text';console.log(foo + ' 是内插的.');

I am to trying to learn Express library and Node.js one step at a time. First I am looking at is the specifics of the Node reqiure(moduleName) function.

I took a look at the documentation for this, and found some weird code in the example documentation:

const circle = require('./circle.js');
console.log( `The area of a circle of radius 4 is ${circle.area(4)}`);

More specifically the ${circle.area(4)} bit.

From what I understand the $ in JavaScript is just like any other variable. When we are using it on client side web development it is used as a delegate for the document function (I think). What is it assigned to when using node?

On top of that, what does this syntax mean? ${circle.area(4)}

If $ is just a reference to some function someFunction(), wouldn't it be equivalent to this someFunction(){cirle.area(4)}. I am not seeing how that could be valid syntax.

Also, why wouldn't they just directly call the circle.area() function directly anyways?

解决方案

This:

`The area of a circle of radius 4 is ${circle.area(4)}`

is an example of ES2015 template strings.

It interpolates whatever circle.area(4) represents directly into the string. If you're curious about this or other ES2015 features, I recommend checking out Babel and playing around in the REPL.

Here's a very simple example to get you started.

You can see this ES2015 code:

const foo = 'some text';
console.log(`${foo} is interpolated.`);

is transpiled to its ES5 equivalent - a simple + concatenation:

var foo = 'some text';
console.log(foo + ' is interpolated.');

这篇关于节点文档中的这个 `...${...}...` 代码是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆