Last active
October 18, 2021 02:47
-
-
Save Alonso-Pablo/417a025d28a9334cd417659b6249915f to your computer and use it in GitHub Desktop.
Add a method inside the prototype of Number or String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const aNumber = 13254 | |
const aString = 'aa' | |
// We add a method in the prototype of Number and String | |
Number.prototype.sort = function() { | |
return Number(this.toString().split('').sort((a,b)=>a-b).join('')) | |
} | |
String.prototype.capitalize = function() { | |
return this.charAt(0).toUpperCase() + this.slice(1) | |
} | |
console.log(aNumber.sort()) // Expected: 12345 | |
console.log(aString.capitalize()) // Expected: 'Aa' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment