Home  >  Q&A  >  body text

node.js - nodejs `${}` 取值用法

刚刚接触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中文网2762 days ago480

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 13:52:17

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

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 13:52:17

    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

    reply
    0
  • Cancelreply