在 JavaScript 中建立自定義 toLowerCase() 函式
我們需要編寫一個 JavaScript 字串函式來覆蓋預設 toLowerCase(),該函式應與預設函式具有相同的功能。
我們來編寫此函式的程式碼 −
示例
const str = 'Some UpPerCAsE LeTTeRs!!!';
const toLowerCase = function(){
let str = '';
for(let i = 0; i < this.length; i++){
const ascii = this[i].charCodeAt();
if(ascii >= 65 && ascii <= 90){
str += String.fromCharCode(ascii + 32);
}else{
str += this[i];
};
};
return str;
};
String.prototype.toLowerCase = toLowerCase;
console.log(str.toLowerCase());輸出
控制檯中的輸出將為 −
some uppercase letters!!!
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP