-
Notifications
You must be signed in to change notification settings - Fork 26
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
2015年3月31日,来做一道前端笔试题 #44
Comments
String.prototype.render = function(option){
var s = this,
reg;
Object.keys(option).forEach(function(k){
reg = new RegExp("\\$\\("+k+"\\)","g");
s = s.replace(reg,option[k])
});
return s
};
var greeting = 'my name is $(name),age $(age)';
var result = greeting.render({name:'XiaoMing',age:11});
console.log(result); // my name is XiaoMing,age 11 |
String.prototype.render = function (obj) {
var str = this, reg;
Object.keys(obj).forEach(function (v) {
reg = new RegExp('\\$\\('+ v +'\\)', 'g');
str = str.replace(reg, obj[v]);
});
return str;
}
var greeting = 'my name is $(name),age $(age)';
var result = greeting.render({name:'XiaoMing',age:11});
console.log(result); // my name is XiaoMing,age 11 感觉会有bug。 |
@think2011 举个栗子?⊙﹏⊙ |
@VaJoy做过类似的题目,但是没通过测试,也是上面那样写的.. 后来没做完,有空找找那道题。 |
|
我就传个low low的做法吧 |
es6的字符串吧 var obj = {name:'XiaoMing',age:11}
`my name is ${obj.name},age ${obj.age}` |
String.prototype.render=function(object){
return this.replace(/(?:\$\()(\w+)(?:\))/g,function(all,word){
return object[word]||''
});
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: