Created
June 20, 2013 05:39
-
-
Save ynonp/5820536 to your computer and use it in GitHub Desktop.
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
/** | |
* Namespaces in JavaScript are defined using objects | |
* We'll start from the global object and define nested | |
* namespaces within. | |
* | |
* All namespaces are accessible from every other JS file using | |
* their fully qualified name starting from global. | |
* i.e. global.myapp.data.number = 5; | |
*/ | |
(function(global) { | |
// First level nesting | |
global.myapp = {}; | |
// Second level nesting | |
global.myapp.views = {}; | |
global.myapp.data = {}; | |
}(this)); |
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
/** | |
* A simple class pattern implemented using | |
* a function in JavaScript | |
*/ | |
(function(global) { | |
// Can write any code here as static class constructor | |
// It'll get executed only once | |
var STATIC_TEXT = "Hello"; | |
// Defining a new class is like writing a constructor | |
// Note the parameters are actually ctor params | |
global.Vehicle = function( wheels ) { | |
// boilerplate object definition - always write this | |
var self = {}; | |
/* Private Instance Methods are defined using var */ | |
// can call fix_engine() from within this scope, but | |
// not from outside | |
var fix_engine = function() { | |
console.log('Fixed!'); | |
}; | |
/* Public Instance Variable self.size */ | |
if ( wheels > 4 ) { | |
self.size = 'BIG'; | |
} else { | |
self.size = 'SMALL'; | |
} | |
/* Public Instance Method self.drive and self.stop */ | |
self.drive = function() { console.log('Vroom' ); }; | |
self.stop = function() { console.log('Stopping...'); }; | |
// boilerplate returning self - always write this | |
return self; | |
}; | |
// To create objects and use them, just call the ctor | |
var c1 = new Car(4); | |
var c2 = new Car(8); | |
// Calling a member function | |
c1.drive(); | |
// Using a public instance varaible | |
console.log( c2.size ); | |
}(this)); |
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
/** | |
* Inheritance is implemented by having one constructor | |
* call its base class constructor. | |
* This gives you an object with both the derived and base properties. | |
* | |
*/ | |
(function(global) { | |
global.Car = function( doors ) { | |
// Calling base ctor | |
var self = new global.Vehicle(4); | |
// Now specifying Car's properties | |
if ( doors === 2 ) { | |
// new public property | |
self.is_cool = 1; | |
} | |
// new Public function | |
self.race = function() { | |
if ( doors !== 2 ) { | |
console.log("Can't race. Too many doors"); | |
} | |
// drive and stop are defined in the base class Vehicle | |
self.drive(); | |
self.stop(); | |
}; | |
return self; | |
}; | |
}(this)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment