Home  >  Article  >  Web Front-end  >  Detailed explanation of JavaScript single column mode and single mode definition and application code

Detailed explanation of JavaScript single column mode and single mode definition and application code

伊谢尔伦
伊谢尔伦Original
2017-07-24 14:01:231347browse

Single mode:

Single is an object used to divide the namespace and organize a group of related properties and methods together. If he can be instantiated, Then he can only be instantiated once.

Features:

can divide the namespace to eliminate the dangers caused by global variables.

Use branching technology to encapsulate differences between browsers.

Can organize the code into a more integrated structure for easier reading and maintenance.

Code implementation:

/*Basic Singleton*/
var Singleton = {
  attribute:true,
  method1:function(){},
   method2:function(){}
};

Application scenario:

The single mode is often used in our daily applications, which is equivalent to encapsulating our code in one , only exposes one entrance, thus avoiding the contamination of all variables.

Single-case pattern

The single-case pattern defines the creation process of an object. This object has only a single instance and provides a global access to it. access point. It can also be said that a singleton is to ensure that a class has only one instance. The implementation method is generally to first determine whether the instance exists. If it exists, return it directly. If it does not exist, create it and then return it. This ensures that a class has only one instance object.

Code implementation:

There are many ways to implement singletons. Only one of them is introduced below. Use closure method to implement singletons. The code is as follows:

var single = (function(){
  var unique;
  function getInstance(){
    // 如果该实例存在,则直接返回,否则就对其实例化
    if( unique === undefined ){
      unique = new Construct();
    }
    return unique;
  }
  function Construct(){
    // ... 生成单例的构造函数的代码
  }
  return {
    getInstance : getInstance
  }
})();

In the above code, unique is the reference to the returned object, and getInstance is the static method to obtain the instance. Construct is the constructor that creates an instance.

You can obtain a single instance through single.getInstance(), and the same single instance will be obtained every time you call it. This is what the singleton pattern achieves.

Usage scenarios:

The singleton mode is a commonly used mode. There are some objects that we often only need one, such as the global cache and the browser's window object. In js development, the singleton pattern is also widely used. Just imagine, when we

click the login button, a login box will appear on the page, and this floating window is unique. No matter how many times we click the login button, this floating window will only be created once. . Therefore, this login floating window is suitable for singleton mode.

Summarize its usage scenarios:

1. You can use it to divide the namespace

2. With the help of the singleton mode, the code can be organized more consistently. Easy to read and maintain

The above is the detailed content of Detailed explanation of JavaScript single column mode and single mode definition and application code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn