[CoffeeScript] Level 6: Object Orientation
Classes - Part I
Create a Coffee class that will produce coffee objects. In that class, create a constructor that takes name and level as arguments and sets them as instance variables. Also, make sure you create a function called isRussian.
class Coffee constructor : (@name='Russian', @level=2) -> @name = 'Russian' @level = 2 isRussian: -> "#{@name} is Russian"
Classes - Part II
For an already existing Coffee class, create a new coffee object passing in your name (string) and any number (as the level), then assign it to a variable named coffee.
coffee = new Coffee('Zhentian Wan', 9)
Property Arguments
Refactor the constructor to use property arguments. Also, set @level to be an optional argument by setting its default value to 0.
class Coffee constructor: (@name, @level=0) -> isRussian: -> @name is 'Russian'
Class Inheritance
Make the Coffee class inherit from Drink and override the serve method to return false if@sleeve is false, otherwise invoke the superclass method. (Note: The Drink class is defined below).
class Drink sleeve: null serve: -> alert('Pouring drink')
Answer:
class Coffee extends Drink constructor: (@name, @level=0) -> serve: -> return false if @sleeve is not true super()
Classes with jQuery
On the DrinkLink class below, implement the watchClick method so that when any link is clicked, its color is changed to #F00.
class DrinkLink watchClick: -> $('a').click (event) -> event.preventDefault() $(@).css(color: '#F00')
Watch those @'s
Fix the bug on the code below, which is causing the @linkClicked variable to not be properly set when a link is clicked.
class DrinkLink constructor: (@linkClicked=false) -> watchClick: -> $('.drink a').click (event) -> $(event.target).css('color', '#F00') @linkClicked = true
Answer:
class DrinkLink constructor: (@linkClicked=false) -> watchClick: -> $('.drink a').click (event) => $(event.target).css('color', '#F00') @linkClicked = true

浙公网安备 33010602011771号