A single object is an object used to divide namespaces and organize a batch of related methods and attributes. If it can be instantiated, it can only be instantiated once.
The single mode organizes the code into a logical unit. The code in this logical unit can be accessed through a single variable.
The basic structure of the monomer is as follows:
var Singleton = { attribute1:true, attribute2:10, method1:function(){}, method2:function(){}}
Implement monomer with closures:
Namespace.Singleton = {}
The single function that is immediately executed after definition:
Namespace.Singleton = (function(){ return { publicAttr:1, ...};})();
We add the function packaging layer to use the packaging function to add the closure of a real private member.
Implementation of closure monomer:
GiantCorp.DataParser = (function(){ var whitespaceRegex = /\s+/; function stripWhitespace(str){ return str.replace(whitespaceRegex,‘‘); }; function stringSplit(str,delimiter){ return str.split(delimiter); }; return { stringToArray:function(str,delimiter,stripWS){ if(stripWS){ str = stripWhitespace(str); } var outputArray = stringSplit(str,delimiter); return outputArray; } }})();
In this function, these private methods and attributes can directly access their names without adding this. Put Private Members in the closure to ensure that they are not used outside the single object. At the same time, we can freely change the Implementation Details of objects. You can also use this method to protect and encapsulate data.
In this way, you can enjoy the benefits of real private members without having to pay any price. This is because a single class will only be instantiated once, and the single mode is the most popular in JavaScript, this is one of the most widely used models.
Inert instantiation of Monomer
The implementation of the Single Mode mentioned above has one thing in common: the single object is created when the script is loaded.
For resource-intensive or configuration-intensive monomer, it may be more reasonable to postpone its instantiation until it needs to be used. This technology is called inert loading. It is most commonly used for the units that must load a large amount of data.
The following shows how to convert a common monomer into a inert loaded monomer:
Mynamespace. Singleton = (function () {var uniqueinstance; function Constructor () {...} return {getinstance: function () {If (! Uniqueinstance) {// uniqueinstance = Constructor ();} return uniqueinstance ;}}) (); // call mynamespace. singleton. getinstance (). publicemethod1 ();
Branch: a branch is a technology used to encapsulate the differences between browsers into dynamic processing during running.
The following describes how to use the branch technology to create xhr objects:
var SimpleXhrFactory =(function(){ var standard = { createXhrObject:function(){ return new XMLHttpRequest(); } }; var activeNew = { createXhrObject:function(){ return new ActiveXObject(‘Msxml2.XMLHTTP‘); } }; var activeOld = { createXhrObject:function(){ return new ActiveXObject(‘Microsoft.XMLHTTP‘); } }; var testObject = null; try{ testObject = standard.createXhrObject(); return standard; } catch(e){ try{ testObject = activeNew.createXhrObject(); return activeNew; } catch(e){ try{ testObject = activeOld.createXhrObject(); return activeOld; } catch(e){ throw new Error(‘No XHR object found in this environment.‘); } } }})();
In large projects, a single component can be optimized: components with large overhead but seldom used can be encapsulated into a inert loaded monomer; code for the characteristic environment can be packaged into the branch type monomer.