Created
December 31, 2014 04:01
-
-
Save jacobdam/e201d821193ebf40c3be to your computer and use it in GitHub Desktop.
JS OOP
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
__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; | |
}; |
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 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() |
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 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