Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save goranobradovic/3104233 to your computer and use it in GitHub Desktop.
Save goranobradovic/3104233 to your computer and use it in GitHub Desktop.
Partial class with partial constructor in JS
// 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();
@goranobradovic
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment