Skip to content

Instantly share code, notes, and snippets.

@jacobdam
Created December 31, 2014 04:01
Show Gist options
  • Save jacobdam/e201d821193ebf40c3be to your computer and use it in GitHub Desktop.
Save jacobdam/e201d821193ebf40c3be to your computer and use it in GitHub Desktop.
JS OOP
__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;
};
class Dog
constructor: (@isHungry = false)->
bark: ->
if @isHungry
console.log('Wolf!')
else
console.log('Wolf! Wolf! Wooooolf!')
class BadDog extends Dog
constructor: ->
super(true)
bark: ->
super()
@bite()
bite: ->
console.log('@#%$%^%#@')
dog = new BadDog()
dog.bark()
var BadDog, Dog, dog,
__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; };
Dog = (function() {
function Dog(isHungry) {
this.isHungry = isHungry != null ? isHungry : false;
}
Dog.prototype.bark = function() {
if (this.isHungry) {
return console.log('Wolf!');
} else {
return console.log('Wolf! Wolf! Wooooolf!');
}
};
return Dog;
})();
BadDog = (function(_super) {
__extends(BadDog, _super);
function BadDog() {
BadDog.__super__.constructor.call(this, true);
}
BadDog.prototype.bark = function() {
BadDog.__super__.bark.call(this);
return this.bite();
};
BadDog.prototype.bite = function() {
return console.log('@#%$%^%#@');
};
return BadDog;
})(Dog);
dog = new BadDog();
dog.bark();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment