2017-10-18 75 views
-1

在下面的代碼中,我有點了解底部,但我從未見過replaceAt。我只見過replacecharAt。那麼什麼是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(' '); 
} 
+3

的可能的複製[?這怎麼replaceAt功能工作(https://stackoverflow.com/questions/45148658/如何做這個替換功能工作) – jmargolisvt

+0

學習[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

回答

0

Funcion名replaceAt是一個自定義名稱替換字符串的字符,你可以將其命名爲您like,如myReplaceAt

製作String.prototype功能時,this裏面的函數是應用str荷蘭國際集團,像這樣的例子:

var myString = "test" 
myString.myReplaceAt(... 

myString的價值是thismyReplaceAt

+0

我以爲'。這個'只能用於物體? – ajhernandez95

0

String.prototype.replaceAt字符串的方法

允許你在一個特定的指數

String.prototype.replaceAt=function(index, char) {return this.substr(0, index) + char + this.substr(index+char.length);}