Last active
August 29, 2015 14:26
-
-
Save Yama-to/cbbb7409ea051f9095b4 to your computer and use it in GitHub Desktop.
JavaScriptを卒業してCoffeeScriptで飛躍しよう - 基本などまとめ ref: http://qiita.com/Yama-to/items/a947cfa2565b03ddc9de
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Fruit | |
constructor: (@name) -> | |
hello_world: -> alert "Hello #{@name}!" | |
class Apple extends Fruit | |
hello_world: -> super(); alert "...said Bob." | |
sungold = new Apple "SunGold" | |
sungold.hello_world() | |
#=> "Hello SunGold!" | |
#=> "...said Bob." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Apple, Fruit, sungold, | |
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, | |
hasProp = {}.hasOwnProperty; | |
Fruit = (function() { | |
function Fruit(name) { | |
this.name = name; | |
} | |
Fruit.prototype.hello_world = function() { | |
return alert("Hello " + this.name + "!"); | |
}; | |
return Fruit; | |
})(); | |
Apple = (function(superClass) { | |
extend(Apple, superClass); | |
function Apple() { | |
return Apple.__super__.constructor.apply(this, arguments); | |
} | |
Apple.prototype.hello_world = function() { | |
Apple.__super__.hello_world.call(this); | |
return alert("...said Bob."); | |
}; | |
return Apple; | |
})(Fruit); | |
sungold = new Apple("SunGold"); | |
sungold.hello_world(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment