Skip to content

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 2 commits into from
Sep 18, 2014
Merged
Show file tree
Hide file tree
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
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).

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 or undefined)?

Copy link
Contributor Author

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 works only for primitive properties (=
|number|, |string|, |boolean|, |null| or |undefined|)?
I wouldn't emphazise on that, since it is only a very special case, and
the constructor-pattern is failsafe.


Reply to this email directly or view it on GitHub
https://github.com/coffeescript-cookbook/coffeescript-cookbook.github.com/pull/121/files#r17658658.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 - agreed :)


{% 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.
43 changes: 0 additions & 43 deletions chapters/classes_and_objects/class-variables.md

This file was deleted.