Skip to content

Commit 619d317

Browse files
committed
Merge pull request #80 from michaelglass/fix_class_attributes_lesson
lesson on class variables is incorrect.
2 parents a10e302 + 2be90d1 commit 619d317

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Diff for: chapters/classes_and_objects/class-variables.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,29 @@ class Zoo
1515
MAX_ZOOKEEPERS: 3
1616

1717
helpfulInfo: =>
18-
"Zoos may contain a maximum of #{@constructor.MAX_ANIMALS} animals"
18+
"Zoos may contain a maximum of #{@constructor.MAX_ANIMALS} animals and #{@MAX_ZOOKEEPERS} zoo keepers."
1919

2020
Zoo.MAX_ANIMALS
2121
# => 50
2222

2323
Zoo.MAX_ZOOKEEPERS
24-
# => undefined (it is an instance variable)
24+
# => undefined (it is a prototype member)
25+
26+
Zoo::MAX_ZOOKEEPERS
27+
# => 3
2528

2629
zoo = new Zoo
2730
zoo.MAX_ZOOKEEPERS
2831
# => 3
2932
zoo.helpfulInfo()
30-
# => "Zoos may contain a maximum of 50 animals"
33+
# => "Zoos may contain a maximum of 50 animals and 3 zoo keepers."
34+
35+
zoo.MAX_ZOOKEEPERS = "smelly"
36+
zoo.MAX_ANIMALS = "seventeen"
37+
zoo.helpfulInfo()
38+
# => "Zoos may contain a maximum of 50 animals and smelly zoo keepers."
3139
{% endhighlight %}
3240

3341
## Discussion
3442

35-
Coffeescript will store these values on the object itself rather than on the object prototype (and thus on individual object instances), which conserves memory and gives a central location to store class-level values.
43+
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.

0 commit comments

Comments
 (0)