JavaScript design pattern--Singleton mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.