zoukankan      html  css  js  c++  java
  • ES6知识点整理之----字符串扩展----模板字符串

    模板字符串(template string)是增强版的字符串,用反引号(`)标识。它可以当作普通字符串使用,也可以用来定义多行字符串,或者在字符串中嵌入变量。

    // 普通字符串
    `In JavaScript '
    ' is a line-feed.`
    
    // 多行字符串
    `In JavaScript this is
     not legal.`
    
    // 字符串中嵌入变量
    let name = "Bob", time = "today";
    `Hello ${name}, how are you ${time}?`

    模板字符串中嵌入变量,需要将变量名写在${}之中。

    function authorize(user, action) {
      if (!user.hasPrivilege(action)) {
        throw new Error(
          // 传统写法为
          // 'User '
          // + user.name
          // + ' is not authorized to do '
          // + action
          // + '.'
          `User ${user.name} is not authorized to do ${action}.`);
      }
    }

    模板字符串还能嵌套。

    const tmpl = addrs => `
      <table>
      ${addrs.map(addr => `
        <tr><td>${addr.first}</td></tr>
        <tr><td>${addr.last}</td></tr>
      `).join('')}
      </table>
    `;
  • 相关阅读:
    Python与mongo交互
    MongoDB数据库操作
    爬虫之xpath解析库
    selenium常用操作
    无头浏览器的使用
    BeautifulSoup库使用
    urllib简单介绍
    爬虫自动化工具防检测
    支付宝支付
    TortoiseSVN使用教程[多图超详细]
  • 原文地址:https://www.cnblogs.com/adhehe/p/9643013.html
Copyright © 2011-2022 走看看