Created
April 20, 2015 20:44
-
-
Save twinsdbv/260baf556cc6514fb622 to your computer and use it in GitHub Desktop.
Javascript OOP - prototype in IIF
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 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