Last active
August 29, 2015 14:04
-
-
Save marshluca/8d242fce7c3c6461ec1e to your computer and use it in GitHub Desktop.
Extending Built-in Objects in CoffeeScript
This file contains hidden or 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
# Use :: to assign your new function to the prototype of the object or class. | |
# Maintainable JavaScript: Don’t modify objects you don’t own; Extending built-in native objects. Evil or not? | |
# http://www.slideshare.net/nzakas/maintainable-javascript-1071179 | |
unless String::trim then String::trim = -> @replace /^\s+|\s+$/g, "" | |
String::strip = -> if String::trim? then @trim() else @replace /^\s+|\s+$/g, "" | |
String::lstrip = -> @replace /^\s+/g, "" | |
String::rstrip = -> @replace /\s+$/g, "" | |
" padded string ".trim() | |
# => 'padded string' | |
" padded string ".strip() | |
# => 'padded string' | |
" padded string ".lstrip() | |
# => 'padded string ' | |
" padded string ".rstrip() | |
# => ' padded string' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment