在下面的代碼中,我有點了解底部,但我從未見過replaceAt
。我只見過replace
和charAt
。那麼什麼是replaceAt
?請解釋這段代碼的頂部,具體是replaceAt部分
此外,我在this
部分有一個問題,以瞭解什麼this
實際上捕獲。
String.prototype.replaceAt = function(index, character) {
return this.substr(0, index) + character + this.substr(index+character.length);
};
function titleCase(str) {
var newTitle = str.split(' ');
var updatedTitle = [];
for (var st in newTitle) {
updatedTitle[st] = newTitle[st].toLowerCase().replaceAt(0, newTitle[st].charAt(0).toUpperCase());
}
return updatedTitle.join(' ');
}
的可能的複製[?這怎麼replaceAt功能工作(https://stackoverflow.com/questions/45148658/如何做這個替換功能工作) – jmargolisvt
學習[MDN上的繼承和原型鏈](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain)可能有助於解釋爲什麼你以前從未見過'replaceAt'。 ['this'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this)是JavaScript函數的特殊關鍵字。 – jacefarm