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

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

问题描述

我正在尝试一次学习Express库和Node.js。首先我正在查看节点的细节 reqiure(moduleName)函数。



我看了看文档,并在示例文档中找到一些奇怪的代码:

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

更具体地说, $ {circle.area(4)} bit。



从我所理解的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等价物 - 一个简单的 + concatenation:

  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天全站免登陆