js模板语法前的函数

2021-06-01  本文已影响0人  Wrestle_Mania
    let lesson = [
      {
        title: "语文",
        author: "张三",
      },
      {
        title: "数学",
        author: "李四",
      },
      {
        title: "英语",
        author: "王五",
      },
    ];

    function template() {
      return `
      <ul>${lesson
        .map((item) => links`<li>课程:${item.title} 老师:${item.author}</li>`)
        .join("")}</ul>`;
    }

    function links(strings, ...vars) {
      return strings.map((item, index) => item + (vars[index] || ""));
    }
上一篇 下一篇

猜你喜欢

热点阅读