Skip to content

added explanation for prototype class variables #110

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

Closed
wants to merge 1 commit into from
Closed

added explanation for prototype class variables #110

wants to merge 1 commit into from

Conversation

wilzbach
Copy link

I stumbled across the problem of accidentally creating shared/static variables. I seems that I am not the first one - so I thought it might be a good idea to correct this cookbook ;-)
The recipe only works for primitive variables.

@michaelglass
Copy link
Member

I think if you want to return the same value for a mutable object you should just

# use a getter
Foo.getButts = ->
  ['butts']

@wilzbach
Copy link
Author

Actually my point was to show how to prevent sharing of non-primitive variables. If you follow the original recipe than the variables would behave like favoriteFoodShared, which is probably not the intended behavior.

@michaelglass
Copy link
Member

but the section you're adding it to is the class variable section. And that functionality is already demonstrated here, I believe.

Happy to defer to a second opinion though.

@wilzbach
Copy link
Author

I first wanted to add it there, but this is really about class variables. As I said before the original recipe explains how to have MAX_ZOOKEEPERS as a class variables.
However this won't work as expected with arrays etc.

It seems to be that at least it is not clear at the first glance what I want to show with this extension? I am open to any suggestions :-)

@michaelglass
Copy link
Member

I think (maybe I'm reading it wrong) that it's demonstrating the difference between MAX_ANIMALS (a class variable) and MAX_ZOOKEEPERS (a prototyped variable)

@wilzbach
Copy link
Author

Yep as far as I understand is MAX_ANIMALS shared between all instances (aka static) and MAX_ZOOKEEPERS is a instance variable.

Should I use MAX_ZOOKEEPERS instead of favoriteFoodShared?
All I wanted to show and warn is about the instance variable MAX_ZOOKEEPERS.

@wilzbach
Copy link
Author

here is an example why I consider this tricky for beginners and the solution to instantiate the immutable variable in the constructor is needed. If you come from any other non-prototyped language you would expect tiger.mutable to be []

http://jsfiddle.net/3Fsbc/1/

@pbihler
Copy link
Contributor

pbihler commented Sep 17, 2014

I think, you had some misunderstandings here due to misleading word use. The example provided by @greenify was about problems/pitfalls on instance variables. #121 solves the issue by extending the class-variables to a class-variables-and-instance-variables page (parallel to the *-methods page).

@wilzbach
Copy link
Author

thanks @pbihler for #121. Exactly what I wanted to explain :)

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

Successfully merging this pull request may close these issues.

3 participants