Skip to content

Instantly share code, notes, and snippets.

@pulsation
Last active December 11, 2015 16:09
Show Gist options
  • Save pulsation/4625946 to your computer and use it in GitHub Desktop.
Save pulsation/4625946 to your computer and use it in GitHub Desktop.
Experimenting with javascript closures: inheritance
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