Created
April 4, 2012 13:11
-
-
Save mattjmorrison/2300965 to your computer and use it in GitHub Desktop.
JavaScript: Roll your own OO
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
jQuery.noConflict(); | |
MyClass = function($){ | |
return function(val){ | |
console.log("Constructing MyClass Instance"); | |
var self = {}; | |
self.getVal = function(){ | |
return self.val; | |
}; | |
self.val = val; | |
return self; | |
}; | |
}(jQuery); | |
var myFirstInstance = MyClass("a"); | |
var mySecondInstance = MyClass("b"); | |
console.log(myFirstInstance.getVal()); | |
console.log(mySecondInstance.getVal()); | |
|
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
jQuery.noConflict(); | |
MyModule = function($){ | |
var pub = {}; | |
pub.MyClass = function(val){ | |
console.log("Constructing MyClass Instance with " + val); | |
this.val = val; | |
}; | |
pub.MyClass.prototype.getVal = function(){ | |
return this.val; | |
}; | |
return pub; | |
}(jQuery); | |
var myFirstInstance = new MyModule.MyClass("a"); | |
var mySecondInstance = new MyModule.MyClass("b"); | |
console.log(myFirstInstance.getVal()); | |
console.log(mySecondInstance.getVal()); | |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment