• ES6 模板编译


    顾名思义,就是用反引号编写一个模板字符串,

    用echo将模板转为javascrip表达式字符串,

    用正则将基础字符串转为想要字符串

    将代码封装在函数中返回;

    注:

    用到es6属性${}

    var template=`

        <ul>

        <%for(var i=0;i<data.supplies.length;i++){%>

            <li><%=data.supplies[i]%> </li>

        <%}%>

    </ul>`;

    function compile(template){

         var  reg1=/<%=(.+?)%>/g;

         var reg2=/<%([sS]+?)%>/g;

         template=template

          .replace(reg1,'`); echo($1); echo(`')

          .replace(reg2,'`);  $1 echo(`');

          template='echo(`'+template+'`)';

         var script=`

               (function parse(data){

                   var outpput='';

                   function echo(html){

                         output+=html

                   }

                ${template}

                return output;

              )}`

          return script

    }

    var parse=eval(compile(template));

    div.innerHTML=parse({supplies:['1',,'2','3']});

  • 相关阅读:
    pyQt5新手教程 (二)开始你的旅程-编写
    未来写作
    电脑使用问题
    cad学习问题
    chm制作
    github学习之路
    python解决实际问题
    英语学习
    经济学
    通过playgrounds程序学习swift语言及思想
  • 原文地址:https://www.cnblogs.com/wdxue/p/6919823.html
Copyright © 2020-2023  润新知