Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion chapters/classes_and_objects/class-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,51 @@ zoo.helpfulInfo()

## Discussion

Coffeescript will store these values on the class itself rather than on the prototype it defines. These are useful for defining variables on classes which can't be overrided by instance attribute variables.
Coffeescript will store these values on the class itself rather than on the prototype it defines.
These are useful for defining variables on classes which can not be overrided by instance attribute variables.

## Warning

Be aware that for non-primitive (mutable) properties (anything execpt `number`, `string`, `boolean`, `null` or `undefined`)
so (e.g. an array `[]` or a dictionary `{}`) the variable will be shared among instances.

## Problem

You want to create a class variable for non-primitive type.

## Solution

Create the variable in the constructor


{% highlight coffeescript %}
class Animal

# variable is mutable and will be shared across all instances of the prototype
favoriteFoodShared : []

constructor: ->
@favoriteFood = []

bear = new Animal()
lion = new Animal()

lion.favoriteFood.push "Zebras"

lion.favoriteFood
# => ["Zebras"]

bear.favoriteFood
# => []

# here is what happens if you would use modify the mutable variable

lion.favoriteFoodShared.push "Zebras"

lion.favoriteFoodShared
# => ["Zebras"]

bear.favoriteFoodShared
# => ["Zebras"]

{% endhighlight %}