如何正确地将括号添加到此代码中
这段代码修剪了空格,(仅供参考:它被认为非常快)
function wSpaceTrim(s){
var start = -1,
end = s.length;
while (s.charCodeAt(--end) < 33 ); //here
while (s.charCodeAt(++start) < 33 ); //here also
return s.slice( start, end + 1 );
}
while 循环没有括号,我如何正确地将括号添加到这段代码中?
while(iMean){
// like this;
}
非常感谢!
This code trims whitespace, (fyi: it's credited to be very fast)
function wSpaceTrim(s){
var start = -1,
end = s.length;
while (s.charCodeAt(--end) < 33 ); //here
while (s.charCodeAt(++start) < 33 ); //here also
return s.slice( start, end + 1 );
}
The while loops don't have brackets, how would i correctly add brackets to this code?
while(iMean){
// like this;
}
Thank you so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
循环体是空的(实际发生的是循环条件内的递增/递减操作),因此只需添加
{}
:相同 while 循环的更长且可能更容易阅读的版本将是:
The loop bodies are empty (the actual thing that happens is the increment/decrement operation within the loop condition), so simply add
{}
:A longer, and probably easier to read version of the same while loop would be:
该代码不需要括号,但确实需要一个使用本机修剪方法的选项。
Opera、Firefox 和 Chrome 都有原生的字符串原型修剪功能——
其他浏览器也可以添加它。
对于这个特殊的方法,我想我会稍微使用一下 String.prototype,
以便尽可能使用内置方法。
这可能确实很快,但我更喜欢简单-
The code doesn't need brackets, but it does need an option to use a native trim method.
Opera, Firefox and Chrome all have a native string prototype trim function-
the other browsers could add it as well.
For this particular method, I think I would monkey a bit with the String.prototype,
so as to use the built in method where possible.
This may indeed be fast, but I prefer simple-