我是編程新手,我正在學LearnStreet的「Writing functions」。 我的問題是有以下幾點:Javascript .slice方法(更深入的瞭解)
function capitalizeFirst(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
我得到,如果它只是:
return str.charAt(0).toUpperCase();
只有首字母將返回和資本,但爲什麼加入.slice(1)
給整個字符串返回?
在的情況下:
capitalizeFirst("i am apple");
// "I am apple" is returned
但隨着時間提前了對這個問題的任何幫助/指導
function capitalizeFirst(str) {
return str.charAt(0).toUpperCase() + str.slice(0); // 0 instead of 1
}
capitalizeFirst("i am apple");
// "Ii am apple" is returned
感謝。
[String.slice的文檔](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/slice),不要與[Array.slice]混淆( https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/slice)。 – jbabey