Skip to content

Instantly share code, notes, and snippets.

@clyfe
Created July 16, 2011 14:32
Show Gist options
  • Save clyfe/1086403 to your computer and use it in GitHub Desktop.
Save clyfe/1086403 to your computer and use it in GitHub Desktop.
include Module pattern: copy VS prototype chain injection
_ = require 'underscore'
includeCopy = (to, module) ->
proto = to::
for name, property of module
proto[name] = property
includeInter = (to, module) ->
m = _.clone(module)
originalPrototype = to.prototype
to.prototype = m
m.__proto__ = originalPrototype
# maybe make sure we don't loose the constructor too ?
Courage =
sword: -> console.log 'swording'
guts: 2
class Samurai
# includeCopy @, Courage
# includeInter @, Courage
s = new Samurai()
s.sword()
console.log s.guts
@clyfe
Copy link
Author

clyfe commented Jul 16, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment