Skip to content

Instantly share code, notes, and snippets.

@Alonso-Pablo
Last active October 18, 2021 02:47
Show Gist options
  • Save Alonso-Pablo/417a025d28a9334cd417659b6249915f to your computer and use it in GitHub Desktop.
Save Alonso-Pablo/417a025d28a9334cd417659b6249915f to your computer and use it in GitHub Desktop.
Add a method inside the prototype of Number or String
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