Chúng ta cần viết hàm String ghi đè mặc định toLowerCase() nhưng vẫn có cùng chức năng.
Dưới đây là mã:
Ví dụ
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());Kết quả
Đầu ra trong console sẽ là:
some uppercase letters!!!