Singleton mode: An object class that is only allowed to instantiate once
This can be used frequently in basic programming, but it is not known as a singleton pattern, it is to encapsulate methods and properties into an object.
For example, config.js files, Service.js will often use:
App.factory (' Payinfo ', function($location) { //might use a resource here, returns a JSON array return{getstatusmsg: function(paystatus){ Switch(Paystatus) { Case 0:return "Successful Payment!" "; Case 1:return "Payment failed!" "; Case 2:return "Cancel Payment!" "; }}, Getpaytype: function(type){ Switch(type) { Case ' Ticket ':return 0; Case ' Red ':return 1; Case ' Recharge ':return 2; }}, Gethaspasswordflag: function(){ return 1; }, Gethasredflag: function(){ return 1; }, Getpaypathtype: function(type){ Switch(type) { Case ' account ':return '/paymentaccount.html '; Case ' Red ':return '/paymentred.html '; }}, Getaccounttypeid: function(type){ Switch(type) { Case ' UnionPay ':return 3; Case ' account ':return 1; Case ' Lianlian ':return 6; }}, Clearsuccesslocalstrorage: function(){Localstorage.removeitem (' Paysuccesscontent '); Localstorage.removeitem (' Paysuccesscontentsecond '); }, Getpaysuccessurl: function(){ return ' xxx? '+$location. URL (). Split ('? ')[1]; }}). Factory (' Request ', function($http){ return{sendrequest: function(form_data,successcallback,excallback){Console.log (Form_data);varReq = {method:' POST 'Url:' xxx ', Data:form_data}$http(req). Then ( function(response_data){Successcallback (Response_data); }, function(RESPONSE_EX){Excallback (RESPONSE_EX); }); }}). Factory (' DECODE ', function(){ return{Base: function(decodeurl){ for(varKey in Decodeurl) {Decodeurl[key] = Window.atob (Decodeurl[key]); } } }})
JavaScript design pattern--Singleton mode