Created
April 18, 2011 23:47
-
-
Save adamloving/926542 to your computer and use it in GitHub Desktop.
More examples of javascript inheritance patterns
This file contains hidden or 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
| // There does not appear to be a clear winner here. #3 is ideal but too verbose. | |
| // So, I think it is convenient to use a combination of all of these. | |
| // establish a namespace | |
| Ext.ns('MYCO.controls'); // for class definitions | |
| Ext.ns('MYCO.mypage'); // for instances | |
| // [1] Singleton object with functions (when only 1 object is necesary) | |
| MYCO.mypage.mySimpleCar = { | |
| doors : 4, | |
| drive: function() { console.log('simple car driving'); } | |
| }; | |
| MYCO.mypage.mySimpleCar.drive(); | |
| // [2] But, you cannot have multiple instances of that object... | |
| // so instead use this. | |
| MYCO.mypage.mySimpleCar = function(){ | |
| return { | |
| doors : 4, | |
| drive: function() { console.log('simple car driving'); } | |
| }; | |
| }; | |
| new MYCO.mypage.mySimpleCar().drive(); | |
| // [3] Inheritable Class Definition - if you need inheritance | |
| MYCO.controls.BaseCar = function() {}; | |
| // add methods to the base class using prototype | |
| MYCO.controls.BaseCar.prototype.doors = 4; | |
| MYCO.controls.BaseCar.prototype.drive = function() { console.log('Car with ' + this.doors + ' doors driving.') }; | |
| new MYCO.controls.BaseCar().drive(); // should have 4 doors. | |
| MYCO.controls.SportsCar = function() { | |
| this.doors = this.doors / 2; // OK to override prototype | |
| }; | |
| // set the base class | |
| MYCO.controls.SportsCar.prototype = new MYCO.controls.BaseCar; // kinda weird to have to use 'new' here | |
| // create an instance | |
| MYCO.mypage.myCar = new MYCO.controls.SportsCar(); | |
| MYCO.mypage.myCar.drive(); // should have 2 doors | |
| // [3a] with Ext, use... | |
| MYCO.controls.Limo = Ext.extend(MYCO.controls.BaseCar, { | |
| constructor: function() { | |
| this.doors *= 2; | |
| }, | |
| driveFaster: function() { console.log('driving faster with ' + this.doors + ' doors'); } | |
| }); | |
| new MYCO.controls.Limo().drive(); | |
| new MYCO.controls.Limo().driveFaster(); | |
| // [2b] (A fancier version of 2) | |
| // When creating a page controller (where no inheritance is expected and you only need one instance), this pattern works nicely | |
| MYCO.mycontroller = function() { | |
| // private instance variables | |
| var foo = 33; | |
| // pseudo static initializer | |
| var staticInitializer = function() { // do some code here, will run only once | |
| }(); | |
| // private method | |
| function foobar() { // do something | |
| } | |
| // public api | |
| return { | |
| publicMethod: function() { // do something | |
| }, | |
| publicVariable: 42, | |
| publicMethod2: function() { // do something | |
| } | |
| } | |
| }(); // <- note function is executed to create object |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment