Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mixins example can yield to object collision #38

Open
dperetti opened this issue Jan 12, 2012 · 0 comments
Open

Mixins example can yield to object collision #38

dperetti opened this issue Jan 12, 2012 · 0 comments

Comments

@dperetti
Copy link

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

The example needs to be amended as such :

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant