Ay *_*Rue 2 javascript node.js ecmascript-6 template-strings template-literals
我将尝试一步一步地学习Express库和Node.js.首先我要看的是Node reqiure(moduleName)函数的细节.
我看了一下这个文档,并在示例文档中找到了一些奇怪的代码:
const circle = require('./circle.js');
console.log( `The area of a circle of radius 4 is ${circle.area(4)}`);
Run Code Online (Sandbox Code Playgroud)
更具体地说是${circle.area(4)}位.
根据我的理解,$在JavaScript中就像任何其他变量一样.当我们在客户端Web开发中使用它时,它被用作文档功能的委托(我认为).使用节点时分配的是什么?
最重要的是,这种语法意味着什么? ${circle.area(4)}
如果$只是对某个函数的引用someFunction(),那么它不等同于此someFunction(){cirle.area(4)}.我没有看到这可能是有效的语法.
另外,他们为什么不直接直接调用circle.area()函数呢?
Col*_*ock 12
这个:
`The area of a circle of radius 4 is ${circle.area(4)}`
Run Code Online (Sandbox Code Playgroud)
是ES2015模板字符串的示例.
它将circle.area(4)直接表示的任何内容插入到字符串中.如果您对这个或其他ES2015功能感到好奇,我建议您查看Babel并在REPL中玩游戏.
这是一个非常简单的例子,可以帮助您入门.
你可以看到这个ES2015代码:
const foo = 'some text';
console.log(`${foo} is interpolated.`);
Run Code Online (Sandbox Code Playgroud)
被转换为ES5等价物 - 一个简单的+连接:
var foo = 'some text';
console.log(foo + ' is interpolated.');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6878 次 |
| 最近记录: |