Skip to content

Instantly share code, notes, and snippets.

@sukima
Last active January 31, 2022 16:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sukima/6073227 to your computer and use it in GitHub Desktop.
Save sukima/6073227 to your computer and use it in GitHub Desktop.

Example

In this example we are going to build a simple object that will show inharitence and function.

First lets define our main class.

Animal

The animal is a class used to describe general things about all animals.

class Animal
  constructor: (@name, @color) ->
  growl: -> console.log "#{@name} the #{@color} #{@type} goes Roar!!"

Tiger

An example tiger class.

class Tiger extends Animal
  constructor: ->
    super
    @type = "tiger"

Usage

And now here is an example on how to use these classes:

sneezy = new Tiger("Sneezy", "orange")
sneezy.growl()
// Generated by CoffeeScript 1.6.3
(function() {
var Animal, Tiger, sneezy,
__hasProp = {}.hasOwnProperty,
__extends = 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; };
Animal = (function() {
function Animal(name, color) {
this.name = name;
this.color = color;
}
Animal.prototype.growl = function() {
return console.log("" + this.name + " the " + this.color + " " + this.type + " goes Roar!!");
};
return Animal;
})();
Tiger = (function(_super) {
__extends(Tiger, _super);
function Tiger() {
Tiger.__super__.constructor.apply(this, arguments);
this.type = "tiger";
}
return Tiger;
})(Animal);
sneezy = new Tiger("Sneezy", "orange");
sneezy.growl();
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment