Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

填充代码实现 template 方法 #844

Open
lgwebdream opened this issue Jul 6, 2020 · 3 comments
Open

填充代码实现 template 方法 #844

lgwebdream opened this issue Jul 6, 2020 · 3 comments
Labels
JavaScript teach_tag 编程题 teach_tag 网易 company

Comments

@lgwebdream
Copy link
Owner

var str = "您好,<%=name%>。欢迎来到<%=location%>";
function template(str) {
  // your code
}
var compiled = template(srt);
// compiled的输出值为:“您好,张三。欢迎来到网易游戏”
compiled({ name: "张三", location: "网易游戏" });
@lgwebdream lgwebdream added JavaScript teach_tag 编程题 teach_tag 网易 company labels Jul 6, 2020
@lgwebdream
Copy link
Owner Author

扫描下方二维码,获取答案以及详细解析,同时可解锁800+道前端面试题。

@GolderBrother
Copy link

var str = "您好,<%=name%>。欢迎来到<%=location%>";
function template(str) {
  // your code
  return function (obj) {
    const reg = /<%=([a-zA-Z]+)%>/g;
    return str.replace(reg, (_, $1) => {
      return obj[$1] || '';
    });
  }
}
var compiled = template(str);
// compiled的输出值为:“您好,张三。欢迎来到网易游戏”
const result = compiled({ name: "张三", location: "网易游戏" });
console.log(result); // 您好,张三。欢迎来到网易游戏

@eddy5885
Copy link

function template(str){

return function(obj){
    for(let i in obj){
        str = str.replace(`<%=${i}%>`,obj[i])
    }
    console.log(str)
    return str     
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JavaScript teach_tag 编程题 teach_tag 网易 company
Projects
None yet
Development

No branches or pull requests

3 participants