-
Notifications
You must be signed in to change notification settings - Fork 177
Added Instance variables to cookbook #121
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
chapters/classes_and_objects/class-variables-and-instance-variables.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
--- | ||
layout: recipe | ||
title: Class Variables and Instance Variables | ||
chapter: Classes and Objects | ||
--- | ||
## Problem | ||
|
||
You want to create class variables and instance variables (properties). | ||
|
||
## Solution | ||
|
||
### Class Variables | ||
|
||
{% highlight coffeescript %} | ||
class Zoo | ||
@MAX_ANIMALS: 50 | ||
MAX_ZOOKEEPERS: 3 | ||
|
||
helpfulInfo: => | ||
"Zoos may contain a maximum of #{@constructor.MAX_ANIMALS} animals and #{@MAX_ZOOKEEPERS} zoo keepers." | ||
|
||
Zoo.MAX_ANIMALS | ||
# => 50 | ||
|
||
Zoo.MAX_ZOOKEEPERS | ||
# => undefined (it is a prototype member) | ||
|
||
Zoo::MAX_ZOOKEEPERS | ||
# => 3 | ||
|
||
zoo = new Zoo | ||
zoo.MAX_ZOOKEEPERS | ||
# => 3 | ||
zoo.helpfulInfo() | ||
# => "Zoos may contain a maximum of 50 animals and 3 zoo keepers." | ||
|
||
zoo.MAX_ZOOKEEPERS = "smelly" | ||
zoo.MAX_ANIMALS = "seventeen" | ||
zoo.helpfulInfo() | ||
# => "Zoos may contain a maximum of 50 animals and smelly zoo keepers." | ||
{% endhighlight %} | ||
|
||
|
||
### Instance Variables | ||
|
||
You have to define instance variables (i.e. properties) inside a class' method, initialize your defaults in the constructor. | ||
|
||
{% highlight coffeescript %} | ||
class Zoo | ||
constructor: -> | ||
@animals = [] # Here the instance variable is defined | ||
|
||
addAnimal: (name) -> | ||
@animals.push name | ||
|
||
|
||
zoo = new Zoo() | ||
zoo.addAnimal 'elephant' | ||
|
||
otherZoo = new Zoo() | ||
otherZoo.addAnimal 'lion' | ||
|
||
zoo.animals | ||
# => ['elephant'] | ||
|
||
otherZoo.animals | ||
# => ['lion'] | ||
{% endhighlight %} | ||
|
||
#### WARNING! | ||
*Do not add the variable accidently to the prototype, by defining it outside the constructor* (Even if mentioned [elsewhere](http://arcturo.github.io/library/coffeescript/03_classes.html#content), this does not work as intended, due to the underlying JavaScript prototype concept). | ||
|
||
{% highlight coffeescript %} | ||
class BadZoo | ||
animals: [] # Translates to BadZoo.prototype.animals = []; and is thus shared between instances | ||
|
||
addAnimal: (name) -> | ||
@animals.push name # Works due to the prototype concept of Javascript | ||
|
||
|
||
zoo = new BadZoo() | ||
zoo.addAnimal 'elephant' | ||
|
||
otherZoo = new BadZoo() | ||
otherZoo.addAnimal 'lion' | ||
|
||
zoo.animals | ||
# => ['elephant','lion'] # Oops... | ||
|
||
otherZoo.animals | ||
# => ['elephant','lion'] # Oops... | ||
|
||
BadZoo::animals | ||
# => ['elephant','lion'] # The value is stored in the prototype | ||
{% endhighlight %} | ||
|
||
## Discussion | ||
|
||
Coffeescript will store the values of class variables on the class itself rather than on the prototype it defines. These are useful for defining variables on classes which can't be overwritten by instance attribute variables. |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to mention that it only works for primitive properties (=
number
,string
,boolean
,null
orundefined
)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GPG/PGP Key-ID: 0xE18D34F6,
Qeevee GmbH
Firmensitz: Dahlmannstr. 2, B-IT Center, 53113 Bonn, Germany
Registergericht: Bonn HRB 17466
Geschäftsführer: Holger Mügge, Mark von Zeschau
Umsatzsteuer-Identifikationsnummer: DE267457882
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 - agreed :)