例一function titleCase(str) { var newArr = str.toLowerCase().split(" "); newArr = newArr.map(function(val) { val.replace(val[0],val[0].toUpperCase()); return val; }); return newArr.join(" ");}titleCase("I'm a little tea pot"); //i'm a little tea pot例二function titleCase(str) { var newArr = str.toLowerCase().split(" "); newArr = newArr.map(function(val) { return val.replace(val[0],val[0].toUpperCase());; }); return newArr.join(" ");}titleCase("I'm a little tea pot"); //I`m A Little Tea Pot
1 回答
慕的地6264312
TA贡献1817条经验 获得超6个赞
return val.replace(val[0],val[0].toUpperCase());;
等于
var tmp = val.replace(val[0],val[0].toUpperCase());
return tmp;
并没有改变原来的str 只是将返回的字符串直接return了
添加回答
举报
0/150
提交
取消