Last active
December 11, 2015 16:09
-
-
Save pulsation/4625946 to your computer and use it in GitHub Desktop.
Experimenting with javascript closures: inheritance
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
mynamespace = window.mynamespace || {}; | |
mynamespace.Parent = function (param) { | |
var _privateProperty = param; | |
return { | |
getTwice : function () { | |
return _privateProperty * 2; | |
} | |
}; | |
}; | |
mynamespace.Child = function (param) { | |
var _that = mynamespace.Parent.apply(this, arguments); | |
_that.getHalf = function () { | |
return _that.getTwice() / 4; | |
} | |
return _that; | |
}; | |
(function () { | |
var myParent = new mynamespace.Parent(6); | |
var myChild = new mynamespace.Child(8); | |
console.log(myParent.getTwice()); | |
console.log(myChild.getTwice()); | |
console.log(myChild.getHalf()); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment