Skip to content

Instantly share code, notes, and snippets.

@twinsdbv
Created April 20, 2015 20:44
Show Gist options
  • Save twinsdbv/260baf556cc6514fb622 to your computer and use it in GitHub Desktop.
Save twinsdbv/260baf556cc6514fb622 to your computer and use it in GitHub Desktop.
Javascript OOP - prototype in IIF
var MyObject = (function () {
function extend(Child, Parent) {
var F = function() { }
F.prototype = Parent.prototype
Child.prototype = new F()
Child.prototype.constructor = Child
Child.superclass = Parent.prototype
}
function ParentObject () {
this.var1 = 'value1';
}
ParentObject.prototype.myMethod = function () {
this.var1 = 'value_1_1';
return this;
}
function ChildObject () {
this.var2 = 'value2';
ChildObject.superclass.constructor.apply(this, arguments);
this.myMethod();
return this;
}
extend(ChildObject, ParentObject);
return {
self: ParentObject,
Child: ChildObject
}
})();
var MO1 = new MyObject.self();
var MO2 = new MyObject.Child();
console.log('MyObjects:', MO1, MO2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment