2
我想寫一個簡單的一行方法removeSuffix()
。什麼是最好的方法來使用removeSuffix()?
這裏是我的選擇:
test.slice(0, test.length - len);
或
test.substring(0, test.length - len);
或
test.substr(0, test.length - len);
他們都做給這個測試用例同樣的事情:
len = 1; // length of the suffix to remove
test = "abcd" // test string
當前實現:
function removeSuffix(str, len){
return str.slice(0, str.length - len);
}
選擇哪種你覺得是更具可讀性。從一種方法到另一種方法的表現非常微觀,你永遠不會感覺到差異。 – h2ooooooo
不,Sarath更有效地使用切片。必須與最好的去。它刪除和不需要的減法並用否定來代替它。 –