arcturo/library — Issue #38: Mixins example can yield to object collision
Author: dperetti
Created: 2012-01-12 02:04:37+00:00
State: open

The objects need to be cloned before being added in order to prevent different classes extending the same mixin from referencing the same class properties.

The example needs to be amended as such :

```coffeescript

clone = (obj) ->
if not obj? or typeof obj isnt 'object'
return obj

newInstance = new obj.constructor()

for key of obj
newInstance[key] = clone obj[key]

return newInstance

moduleKeywords = ['extended', 'included']

class Module

@extend: (obj) ->
for key, value of obj when key not in moduleKeywords
@[key] = clone value

obj.extended?.apply(@)
this

@include: (obj) ->
for key, value of obj when key not in moduleKeywords
@::[key] = clone value

obj.included?.apply(@)
this
``