Skip to content

Instantly share code, notes, and snippets.

@Yama-to
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yama-to/cbbb7409ea051f9095b4 to your computer and use it in GitHub Desktop.
Save Yama-to/cbbb7409ea051f9095b4 to your computer and use it in GitHub Desktop.
JavaScriptを卒業してCoffeeScriptで飛躍しよう - 基本などまとめ ref: http://qiita.com/Yama-to/items/a947cfa2565b03ddc9de
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."
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