You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: chapters/classes_and_objects/class-variables.md
+12-4
Original file line number
Diff line number
Diff line change
@@ -15,21 +15,29 @@ class Zoo
15
15
MAX_ZOOKEEPERS: 3
16
16
17
17
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."
19
19
20
20
Zoo.MAX_ANIMALS
21
21
# => 50
22
22
23
23
Zoo.MAX_ZOOKEEPERS
24
-
# => undefined (it is an instance variable)
24
+
# => undefined (it is a prototype member)
25
+
26
+
Zoo::MAX_ZOOKEEPERS
27
+
# => 3
25
28
26
29
zoo = new Zoo
27
30
zoo.MAX_ZOOKEEPERS
28
31
# => 3
29
32
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."
31
39
{% endhighlight %}
32
40
33
41
## Discussion
34
42
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