Last active
August 29, 2015 14:00
-
-
Save tomas-stefano/11354072 to your computer and use it in GitHub Desktop.
Inheritance Javascript from scratch with super call
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 Bar, Foo, | |
__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; | |
}; | |
Foo = (function() { | |
function Foo() { | |
this.foo = 'foo'; | |
} | |
return Foo; | |
})(); | |
Bar = (function(_super) { | |
__extends(Bar, _super); | |
function Bar() { | |
this.bar = 'bar'; | |
Bar.__super__.constructor.apply(this, arguments); | |
} | |
return Bar; | |
})(Foo); | |
var foo = new Foo(); | |
foo.foo; | |
var bar = new Bar(); | |
bar.foo; | |
bar.bar; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment