Skip to content

Instantly share code, notes, and snippets.

@marshluca
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marshluca/8d242fce7c3c6461ec1e to your computer and use it in GitHub Desktop.
Save marshluca/8d242fce7c3c6461ec1e to your computer and use it in GitHub Desktop.
Extending Built-in Objects in CoffeeScript
# 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