Skip to content

Instantly share code, notes, and snippets.

@rblalock
Created October 4, 2012 16:06
Show Gist options
  • Save rblalock/3834630 to your computer and use it in GitHub Desktop.
Save rblalock/3834630 to your computer and use it in GitHub Desktop.
sample patterns
/**
* Javascript example patterns
*/
/**
* Singleton Object
* Should be used for global, static objects. You should not place memory
* intensive items in here unless they are meant to be used throughout the application
* at anytime. If the item is meant to be reused or instantiated multiple times
* a singleton should not be used.
*/
// Example One
var MySingleton = {
init: function() {},
clean: function() {},
isVisible: false,
// etc.
};
// Example Two - Same as above but allows you to execute some code before populating the object
// This function executes as soon as you require or include it the first time
var MySingleton = (function() {
function init() {};
function clean() {};
var isVisible = false;
// Public interface is what is returned
return {
init: init,
clean: clean,
isVisible: isVisible
};
})();
/******************* *******************/
/**
* Object Instantiation / Classical
* This should be used for reusable objects that keep their own instantiated state.
* These can also be used
*/
function myObject(_params) {
// Public interfact
this.init = function() {};
this.clean = function() {};
this.isVisible = false;
// Private interface
var myPrivateProperty = true;
}
// Usage:
var item = new MyObject({ stuff: "here" });
item.clean(); // etc.
// If you wanted to extend the object above:
function extendedObject(_params) {
myObject.call(this, _params);
// Now you get all the public properties and methods from myObject
// You can override myObject by simply replacing:
this.clean = function() {};
}
// Usage:
var item = new extendedObject({ stuff: "here" });
item.init(); // etc.
/******************* *******************/
/**
* Require examples
* These examples show some different approaches for CommonJS modules (require() based files)
*
* NOTES:
*
* CommonJS modules are cached when they are evaluated the first time.
* This means every time you require() a module, the contens aren't continually evaluated like
* Ti.include() does. That means requiring a module numerous times doesn't load it in to memory
* every time.
*
* CommonJS modules are 'stateful' meaning any locally declared vars inside the module will maintain
* their state no matter where you're requiring the module from.
*
* To expose methods or properties publically you can either use
* exports.someProperty / exports.someFunction = function() {}; for individual items
* or you can export by using the module.exports namespace. Examples below
*/
/**
* Singleton CommonJS module
*/
// myModule.js:
var Singleton = {
isVisible: false,
init: function() {},
clean: function() {},
show: function() {
Singleton.isVisible = true;
},
hide: function() {
Singleton.isVisible = false;
}
};
module.exports = Singleton;
// Usage file:
var module = require("myModule");
module.init();
module.show();
alert( module.isVisible ); // true
// Note that if you required this in another file and ran the show() method, isVisible
// will be true since CommonJS modules maintain state.
/******************* *******************/
/**
* Another singleton example using exports.*
* Anything with exports.* are exposed to the module interface
*/
// myModule.js:
exports.getDate = function() {};
exports.convertLatLonByDDMMSS = function() {};
exports.myProperty = "test";
// Usage File:
var module = require("myModule");
module.getDate();
alert( module.myProperty ); // "test"
module.convertLatLonByDDMMSS(); // etc.
/******************* *******************/
/**
* Reusable object meant for instantiation
*/
// myModule.js:
function myObject(_params) {
// Public interface
this.init = function() {};
this.clean = function() {};
this.isVisible = false;
// Private interface
var myPrivateProperty = true;
}
module.exports = myObject;
// Usage file:
var Reusable = require("myModule");
var item1 = new Reusable({ stuff: "here" });
var item2 = new Reusable({ stuff: "here too" });
item1.init(); // etc.
/******************* *******************/
/**
* Extendable example of a reusable object
*/
// myModule.js:
function myObject(_params) {
// Public interface
this.init = function() {};
this.clean = function() {};
this.isVisible = false;
// Private interface
var myPrivateProperty = true;
}
module.exports = myObject;
// myChildModule.js:
var Parent = require("myModule");
function childObject(_params) {
Parent.call(this, _params);
this.newMethod = function() {};
// override parent:
this.init = function() {}; // etc.
}
module.exports = childObject;
// Usage file:
var Reusable = require("myChildModule");
var item1 = new Reusable({ stuff: "here" });
item1.init();
item1.newMethod(); // etc.
/******************* *******************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment