node.js - nodejs `${}` 取值用法
PHP中文网
PHP中文网 2017-04-17 13:50:17
0
2
478

刚刚接触nodejs 今天看了一段代码 ${}这个取值操作不是很懂,求解答
a.js如下

module.exports = {
    publicPath:'abc'
}

b.js如下:

var a = require('./a');

function buildConfig() {
    var b = {
        publicPath: `${a.publicPath}`
    }

    console.log(b);

}


module.exports = buildConfig();

当我执行node b.js的时候发现是可以打印输出a.js里面定义的publicPath的值的。

PHP中文网
PHP中文网

认证0级讲师

reply all(2)
左手右手慢动作

This is actually the template string syntax in ES6. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

伊谢尔伦

The template character

${} can replace many string operations that were difficult to write in the past, such as:

Line break

console.log('string text line 1\n' +
'string text line 2');

Use ${} instead of writing:

console.log(`string text line 1
string text line 2`);

Expression embedding

var a = 5;
var b = 10;
console.log('Fifteen is ' + (a + b) + ' and\nnot ' + (2 * a + b) + '.');

${} written as:

var a = 5;
var b = 10;
console.log(`Fifteen is ${a + b} and
not ${2 * a + b}.`);

There are more usages, please refer to the link to adopt the answer

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!