Skip to content

Instantly share code, notes, and snippets.

@MNBuyskih
Created September 1, 2015 11:43
Show Gist options
  • Save MNBuyskih/3dd19a017eaf90c3b525 to your computer and use it in GitHub Desktop.
Save MNBuyskih/3dd19a017eaf90c3b525 to your computer and use it in GitHub Desktop.
Coffeescript deep object clone
clone = (obj) ->
return obj if not obj? or typeof obj isnt 'object'
return new Date(obj.getTime()) if obj instanceof Date
if obj instanceof RegExp
flags = ''
flags += 'g' if obj.global?
flags += 'i' if obj.ignoreCase?
flags += 'm' if obj.multiline?
flags += 'y' if obj.sticky?
return new RegExp(obj.source, flags)
newInstance = new obj.constructor()
newInstance[key] = clone(obj[key]) for key of obj
return newInstance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment