Skip to content

Instantly share code, notes, and snippets.

@chaoticbob
Created August 10, 2014 18:23
Show Gist options
  • Save chaoticbob/4690b92c728a59e629b2 to your computer and use it in GitHub Desktop.
Save chaoticbob/4690b92c728a59e629b2 to your computer and use it in GitHub Desktop.
JavaScript Module Example
///
/// MyNameSpace
///
var MyNameSpace = (function() {
///
/// Constants
///
var Constants = Object.freeze({
PI : 3.141592,
Text : "A random string of text"
});
///
/// SomeObject
///
function SomeObject() {
// private variables
var value = 0;
// public functions
this.getValue = function() {
return value;
}
this.incrementValue = function() {
value++;
}
this.callReplaceMeFunc = function() {
console.log( "SomeObject.callReplaceMeFunc" );
this.replaceMeFunc();
}
}
SomeObject.prototype = {
constructor : SomeObject,
protoFunc : function() {
console.log( "SomeObject.protoFunc" );
},
replaceMeFunc : function() {
console.log( "SomeObject.replaceMeFunc" );
},
protoCallReplaceMeFunc : function() {
console.log( "SomeObject.protoCallReplaceMeFunc" );
this.callReplaceMeFunc();
}
};
///
/// AnotherObject
///
function AnotherObject() {
// private variables
var value = "I am a string";
// public functions
this.getValue = function() {
return value;
}
this.modifyValue = function() {
value = "I am a modified string";
}
}
AnotherObject.prototype = Object.create(
Object.prototype,
{
constructor : AnotherObject,
protoFunc : { value: function() {
console.log( "AnotherObject.protoFunc" );
}}
}
);
///
/// DerivedObject
///
function DerivedObject() {
SomeObject.call( this, arguments );
// private variables
var value = "I am a DerivedObject value";
// public functions
this.getValue = function() {
return value;
}
this.modifyValue = function() {
value = "I am a modified DerivedObject value";
}
}
DerivedObject.prototype = Object.create(
SomeObject.prototype,
{
constructor : DerivedObject,
anotherProtoFunc : { value : function() {
console.log( "DerivedObject.anotherProtoFunc" );
}},
replaceMeFunc : { value : function() {
console.log( "DerivedObject.replaceMeFunc" );
}}
}
);
///
///
///
return {
Constants : Constants,
SomeObject : SomeObject,
AnotherObject : AnotherObject,
DerivedObject : DerivedObject
}
})();
(function() {
console.log( "*** Constants ***" );
console.log( MyNameSpace.Constants.PI );
console.log( MyNameSpace.Constants.Text );
console.log( "\n*** SomeObject ***" );
var so = new MyNameSpace.SomeObject();
console.log( so.getValue() );
so.incrementValue();
console.log( so.getValue() );
so.protoFunc();
console.log( "\n*** AnotherObject ***" );
var ao = new MyNameSpace.AnotherObject();
console.log( ao.getValue() );
ao.modifyValue();
console.log( ao.getValue() );
ao.protoFunc();
console.log( "\n*** DerivedObject ***" );
var dobj = new MyNameSpace.DerivedObject();
console.log( dobj.getValue() );
dobj.modifyValue();
console.log( dobj.getValue() );
dobj.protoFunc();
dobj.anotherProtoFunc();
dobj.callReplaceMeFunc();
dobj.protoCallReplaceMeFunc();
console.log( "\n*** MyCustomObject ***" );
function MyCustomObject() {
MyNameSpace.DerivedObject.call( this, arguments );
}
MyCustomObject.prototype = Object.create(
MyNameSpace.DerivedObject.prototype,
{
myProtoFunc : { value: function() {
console.log( "MyCustomObject.myProtoFunc" );
}}
}
);
var mco = new MyCustomObject();
console.log( mco.getValue() );
mco.modifyValue();
console.log( mco.getValue() );
mco.protoFunc();
mco.anotherProtoFunc();
mco.callReplaceMeFunc();
mco.protoCallReplaceMeFunc();
mco.myProtoFunc();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment