Created
July 13, 2012 10:53
-
-
Save goranobradovic/3104233 to your computer and use it in GitHub Desktop.
Partial class with partial constructor in JS
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
// sample on http://jsfiddle.net/goranobradovic/tvd7F/ | |
// part 1 | |
var MyClass = (function (MyClass) { | |
var self = MyClass; | |
function MyInternalClass(){ | |
function MyMethod() { | |
alert("DoSomething"); | |
}; | |
return { | |
SubMethod: MyMethod | |
}; | |
} | |
function constructor() { | |
self.SubClass = self.SubClass || MyInternalClass(); | |
}; | |
$(document).ready(constructor); | |
return self; | |
}(MyClass || {})); | |
// part 2 | |
var MyClass = (function (MyClass) { | |
var self = MyClass; | |
self.ShowMessage = function () { alert("SHEGA"); } | |
function constructor() { | |
self.SomeProperty = "SomeValue"; | |
}; | |
$(document).ready(constructor); | |
return self; | |
}(MyClass || {})); | |
MyClass.ShowMessage(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is js class prototype which shows how in js you can hawe two partial definitions and two independent constructors for the same final class.
See similar sample on http://jsfiddle.net/goranobradovic/tvd7F/
Result of execution of this script:
MyClass
Object
ShowMessage: function () { alert("SHEGA"); }
SomeProperty: "SomeValue"
SubClass: Object
SubMethod: function MyMethod() {
proto: Object
proto: Object