我想要擴展項目的JavaScript字符串功能,我希望能夠通過函數刪除第一次出現的字符串。
JS不允許使用字符串函數的函數來更改文本。
錯誤我相信是試圖給'this'分配一個文本。
請幫助讓我知道是否有另一種方式來實現這一點。 感謝從字符串對象中刪除第一次出現的字母
// remove first occurrence of a letter from a string
String.prototype.removeFirstMatch = function(char){
\t var text = '';
\t for(i = 0; i < this.length; i++){
\t \t if(this[i] == char){
\t \t \t text = this.slice(0, i) + this.slice(i + 1, this.length);
\t \t }
\t }
\t this = text;
}
var word = 'apple';
word.removeFirstMatch('p');
console.log(word);