function likes (names) { var templates = [ 'no one likes this', '{name} likes this', '{name} and {name} like this', '{name}, {name} and {name} like this', '{name}, {name} and {n} others like this' ]; var idx = Math.min(names.length, 4); return templates[idx].replace(/{name}|{n}/g, function (val) { return val === '{name}' ? names.shift() : names.length; }); } ----------------------------------------------------------------------------- 备注:names是一个存储姓名的数组,如["范冰冰","邓超","王宝强","李健"] 主要是最后一句看不懂。 return val === '{name}' ? names.shift() : names.length; “===”不是只能return ture或false吗?那怎么实现字符串的替换? 求帮忙写下注释,谢谢!
西兰花伟大炮
慕的地8582982
洋葱_dot
相关分类