一:单体的基本结构:
var Singleton = { attribute1: true, attribute2: 10, method1: function() { }, method2: function(arg) { } };
var GiantCorp = {}; GiantCorp.Common = { // A singleton with common methods used by all objects and modules. }; GiantCorp.ErrorCodes = { // An object literal used to store data. }; GiantCorp.PageHandler = { // A singleton with page specific methods and attributes. };
Namespace.PageName = { // Page constants. CONSTANT_1: true, CONSTANT_2: 10, // Page methods. method1: function() { }, method2: function() { }, // Initialization method. init: function() { } }
addLoadEvent(Namespace.PageName.init);
var formID = document.getElementById("form"); formID.addEventListener(‘submit‘,formSubmit,false); function formSubmit(e) { //阻止表单默认动作,然后用ajax发送数据 //接受数据之后调用callBack函数 } function callBack(data) { //用ajax提交表单之后的回调函数
var GiantCorp = window.GiantCorp || {}; GiantCorp.RegPage = { formID: ‘form‘, callBack: function(data) { //用ajax提交表单之后的回调函数 }, formSubmit: function(e) { //阻止表单默认动作,然后用ajax发送数据 //接受数据之后调用callBack函数 }, init: function(e) { GiantCorp.RegPage.formEl = document.getElementById(GiantCorp.RegPage.formID); GiantCorp.RegPage.formEl.addEventListener(‘submit‘,GiantCorp.RegPage.formSubmit,false); } } GiantCorp.RegPage.init();
GiantCorp.DataParser = { // Private methods. _stripWhitespace: function(str) { return str.replace(/\s+/, ‘‘); }, _stringSplit: function(str, delimiter) { return str.split(delimiter); }, // Public method. stringToArray: function(str, delimiter, stripWS) { if(stripWS) { str = this._stripWhitespace(str); } var outputArray = this._stringSplit(str, delimiter); return outputArray; } };
MyNamespace.Singleton = (function() { // Private members. var privateAttribute1 = false; var privateAttribute2 = [1, 2, 3]; function privateMethod1() { ... } function privateMethod2(args) { ... } return { // Public members. publicAttribute1: true, publicAttribute2: 10, publicMethod1: function() { ... }, publicMethod2: function(args) { ... } }; })();
MyNamespace.Singleton = (function() { function constructor() { // All of the normal singleton code goes here. // Private members. var privateAttribute1 = false; var privateAttribute2 = [1, 2, 3]; function privateMethod1() { ... } function privateMethod2(args) { ... } return { // Public members. publicAttribute1: true, publicAttribute2: 10, publicMethod1: function() { ... }, publicMethod2: function(args) { ... } } } })();
MyNamespace.Singleton = (function() { function constructor() { // All of the normal singleton code goes here. ... } return { getInstance: function() { // Control code goes here. } } })();
MyNamespace.Singleton = (function() { var uniqueInstance; // Private attribute that holds the single instance. function constructor() { // All of the normal singleton code goes here. ... } return { getInstance: function() { if(!uniqueInstance) { // Instantiate only if the instance doesn‘t exist. uniqueInstance = constructor(); } return uniqueInstance; } } })();
MyNamespace.Singleton = (function() { var objectA = { method1: function() { ... }, method2: function() { ... } }; var objectB = { method1: function() { ... }, method2: function() { ... } }; return (someCondition) ? objectA : objectB; })();
/* SimpleXhrFactory singleton, step 1. */ var SimpleXhrFactory = (function() { // The three branches. var standard = { createXhrObject: function() { return new XMLHttpRequest(); } }; var activeXNew = { createXhrObject: function() { return new ActiveXObject(‘Msxml2.XMLHTTP‘); } }; var activeXOld = { createXhrObject: function() { return new ActiveXObject(‘Microsoft.XMLHTTP‘); } }; })(); /* SimpleXhrFactory singleton, step 2. */ var SimpleXhrFactory = (function() { // The three branches. var standard = { createXhrObject: function() { return new XMLHttpRequest(); } }; var activeXNew = { createXhrObject: function() { return new ActiveXObject(‘Msxml2.XMLHTTP‘); } }; var activeXOld = { createXhrObject: function() { return new ActiveXObject(‘Microsoft.XMLHTTP‘); } }; // To assign the branch, try each method; return whatever doesn‘t fail. var testObject; try { testObject = standard.createXhrObject(); return standard; // Return this if no error was thrown. } catch(e) { try { testObject = activeXNew.createXhrObject(); return activeXNew; // Return this if no error was thrown. } catch(e) { try { testObject = activeXOld.createXhrObject(); return activeXOld; // Return this if no error was thrown. } catch(e) { throw new Error(‘No XHR object found in this environment.‘); } } } })();
原文:http://www.cnblogs.com/oadaM92/p/4356906.html