栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

推迟执行ES6模板文字

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

推迟执行ES6模板文字

我可以看到三种解决方法:

  • 使用模板字符串,就像设计使用的那样,没有任何
    format
    功能:
    console.log(`Hello, ${"world"}. This is a ${"test"}`);

    // might make more sense with variables:
    var p0 = “world”, p1 = “test”;
    console.log(

    Hello, ${p0}. This is a ${p1}
    );

甚至实际推迟评估的 功能参数

    const welcome = (p0, p1) => `Hello, ${p0}. This is a ${p1}`;console.log(welcome("world", "test"));
  • 不要使用模板字符串,而应使用纯字符串文字:

    String.prototype.format = function() {var args = arguments;return this.replace(/${p(d)}/g, function(match, id) {    return args[id];});

    };
    console.log(“Hello, ${p0}. This is a ${p1}”.format(“world”, “test”));

  • 使用带标签的模板文字。请注意,替换仍将在不被处理程序拦截的情况下进行求值,因此您不能像

    p0
    没有变量so 那样使用标识符。 如果接受了不同的 替换主体语法 建议,则此行为可能会更改(更新:否)。

function formatter(literals, ...substitutions) {return {    format: function() {        var out = [];        for(var i=0, k=0; i < literals.length; i++) { out[k++] = literals[i]; out[k++] = arguments[substitutions[i]];        }        out[k] = literals[i];        return out.join("");    }};}console.log(formatter`Hello, ${0}. This is a ${1}`.format("world", "test"));// Notice the number literals: ^    ^


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/517847.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号