JavaScript OOP
Namespaces in JavaScript
var NAMESPACE = {};
NAMESPACE.example = function() {
var myField = '1';
return {
someMethod: function() {
return myField;
},
anotherMethod: function() {
return myField;
}
};
}();
Prototypal Inheritance
/**
* Allows creation of new objects based on a prototype.
*
* <code>newObject = Object.create(oldObject);</code>
*
* @author Douglas Crockford
* @see http://javascript.crockford.com/prototypal.html
*/
if(typeof Object.create !== 'function')
{
Object.create = function(o)
{
function F(){}
F.prototype = o;
return new F();
};
}
Module
var testModule = function(){
var testModuleProp = function() // Private method{
console.log('[testModuleProp] this: ' + this); // is reference to window object
console.log('[testModuleProp] init: ' + (typeof(init) == 'function' ? 'function' : init)); // function()
if(arguments.callee.test == undefined){
arguments.callee.test = 'done';
init(); // Scope is Window
}
}
var init = function() // Private method only public on testModule because of object mapping of the anonymous functions return statement{
console.log('[init] this: ' + this); // this = object, that holds init function (and gets returned by return {})
console.log('[init] window.browserName: ' + window.browserName);
console.log('[init] testModuleProp: ' + (typeof(testModuleProp) == 'function' ? 'function' : testModuleProp)); // function()
testModuleProp(); // Scope is Window
}
return {init:init};
}();
testModule.init(); // Scope is testModule
/*
==================================================
Inheritance Access: Private, Protected, Privileged and Public
--------------------------------------------------
* fromProto()
Inherits:
BaseClass constructor public and private members
BaseClass prototype members
subClass instanceof Object: true
subClass instanceof SubClass: false
subClass instanceof BaseClass: true
SubClass.prototype.constructor == subClass.constructor: true
subClass.constructor: function BaseClass()
--------------------------------------------------
* BaseClass(); // Within SubClass constructor
Inherits:
BaseClass constructor public members as private members
BaseClass constructor public members can access private members
subClass instanceof Object: true
subClass instanceof SubClass: true
subClass instanceof BaseClass: false
SubClass.prototype.constructor == subClass.constructor: true
subClass.constructor: function SubClass()
--------------------------------------------------
* SubClass.prototype = new BaseClass(); // Prior to constructor
Inherits:
BaseClass constructor public members
BaseClass prototype members
subClass instanceof Object: true
subClass instanceof SubClass: true
subClass instanceof BaseClass: true
SubClass.prototype.constructor == subClass.constructor: true
subClass.constructor: function BaseClass()
--------------------------------------------------
* subclass Function (http://www.golimojo.com/etc/js-subclass.html)
Inherits:
BaseClass constructor public members
SubClass static members enumerable through SubClass.constructor
subClass instanceof Object: true
subClass instanceof SubClass: true
subClass instanceof BaseClass: true
SubClass.prototype.constructor == subClass.constructor: true
subClass.constructor: function SubClass()
--------------------------------------------------
* BaseClass(); // Within SubClass constructor
* SubClass.prototype.__proto__ = BaseClass.prototype; // After or within the SubClass constructor
Inherits:
BaseClass constructor public members
BaseClass prototype members
subClass instanceof Object: true
subClass instanceof SubClass: true
subClass instanceof BaseClass: true
SubClass.prototype.constructor == subClass.constructor: true
subClass.constructor: function SubClass()
--------------------------------------------------
* SubClass.prototype = new BaseClass(); // Prior to constructor
* SubClass.constructor = SubClass; // Prior to constructor
* BaseClass(); // Within SubClass constructor
Inherits:
BaseClass constructor public members
BaseClass constructor public members can access private members
BaseClass prototype members
subClass instanceof Object: true
subClass instanceof SubClass: true
subClass instanceof BaseClass: true
SubClass.prototype.constructor == subClass.constructor: true
subClass.constructor: function BaseClass()
*/