Learning JavaScript design patterns: singleton and javascript Design Patterns

Source: Internet
Author: User

Learning JavaScript design patterns: singleton and javascript Design Patterns

I. Definition

Ensure that a class has only one instance and provides a global access point to it.
When you click the login button, a login floating window appears on the page, which is unique. No matter how many login buttons are clicked, this floating window will only be created once, this login floating window is suitable for creating in singleton mode.

II. Implementation Principle

It is not complex to implement a singleton. A variable is used to indicate whether an object has been created for a class. If yes, the next time you obtain the instance of this class, directly return the previously created object.

3. Case Study

Global variables are not in singleton mode, but we often use global variables as Singleton in JavaScript development.

var a = {};

Reduce naming pollution caused by global variables
(1) Use namespace

var namespace1 = {  a: function(){},  b: 2}

(2) Use closures to encapsulate private variables

var user = (function() {  var _name = 'lee',    _age = '25';  return {    getUserInfo: function() {      return _name + ":" + _age;    }  };})();

4. inert singleton: object instances can be created only when needed

Var getSingle = function (fn) {var result; return function () {return result | (result = fn. apply (this, arguments) ;};}; // test function testSingle () {}getsingle (testSingle) () === getSingle (testSingle) (); // true

5. Supplement:

(1) lazy loading

var lazyload = function() {  console.log(1);  lazyload = function() {    console.log(2);  }  return lazyload();}lazyload();

(2) pre-load

var preload = (function() {  console.log(1);  preload = function() {    console.log(2);  };  return preload;})();preload();

I hope this article will help you learn about javascript programming.

Articles you may be interested in:
  • Javascript Singleton mode DEMO code javascript Object-Oriented Programming
  • Singleton in JavaScript)
  • Two js Singleton Modes
  • Detailed examples of js Singleton Mode
  • Examples of JavaScript Design Patterns in singleton Mode
  • In-depth understanding of the JavaScript series (25): detailed explanation of the singleton mode of the Design Mode
  • Basic usage of JS mode in singleton Mode
  • Simple implementation of javascript 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.