7.1 Singleton

The idea of the singleton pattern is to have only one instance of a specific class. This means that the second time you use the same class to create a new object, you should get the same object that was created the first time.

var obj = {

    myprop: 'my value'

};

var obj2 = {

    myprop: 'my value'

};

obj === obj2; // false

obj == obj2; // false

In JavaScript every time you create an object using the object literal, you’re actually creating a singleton, and there’s no special syntax involved.

7.1.1 Using New

when you use new  to create several objects using the same constructor, you should get only new pointers to the exact same object.

• You can use a global variable to store the instance. This is not recommended because of the general principle that globals are bad. Plus, anyone can overwrite this global variable, even by accident.

• You can cache in a static property of the constructor. Functions in JavaScript are objects,  so  they  can  have  properties.  You  can  have  something  like Universe.instance and cache the object there. This is a nice, clean solution with the only drawback that the instance property is publicly accessible, and code outside of yours might change it, so you lose the instance.

• You can wrap the instance in a closure. This keeps the instance private and not available for modifications outside of your constructor at the expense of an extra closure.

7.1.2 Instance in a Static Property

function Universe() {

    // do we have an existing instance?

    if (typeof Universe.instance  = = = "object") {

        return Universe.instance;

    }

    // proceed as normal

    this.start_time = 0;

    this.bang = "Big";

    // cache

    Universe.instance = this;

    // implicit return:

    // return this;

} 

// testing

var uni = new Universe();

var uni2 = new Universe(); 

uni === uni2; // true

Drawback

Instance is public

7.1.3 Instance in a Closure

// 7.1 Strington - Instance in closure

function Universe() {

    // the cached instance

    var instance = this;

    // proceed as normal

    this.start_time = 0;

    this.bang = "Big";

    // rewrite the constructor

    Universe = function () {

        return instance;

    };

}

Drawback

The rewritten function (in this case the constructor  Universe()) will lose any properties added to it between the moment of initial definition and the redefinition.

// adding to the prototype

Universe.prototype.nothing = true;

var uni = new Universe();

// again adding to the prototype after the initial object is created

Universe.prototype.everything = true;

var uni2 = new Universe();

// only the original prototype was linked to the objects

uni.nothing; // true

uni2.nothing; // true

uni.everything; // undefined

uni2.everything; // undefined

// that sounds right:

uni.constructor.name; // "Universe"

// but that's odd:

uni.constructor === Universe; // false

The reason that uni.constructor is no longer the same as the  Universe() constructor is because  uni.constructor still points to the original constructor, not the redefined one.

// 7.1 Singleton - Advanced Instance in closure

function Universe() {

    // the cached instance

    var instance;

    // rewrite the constructor

    Universe = function Universe() {

        return instance;

    };

    // carry over the prototype properties

    Universe.prototype = this; // this is point to the origin function

    // the instance

    instance = new Universe();    // This is initialized by the origin Universe() constructor.

    instance.constructor = Universe;  // Rewrite the constructor of the instance object.

    // all the functionality

    instance.start_time = 0;

    instance.bang = "Big";

    return instance;

}

// update prototype and create instance

Universe.prototype.nothing = true; // true

var uni = new Universe();

Universe.prototype.everything = true; // true

var uni2 = new Universe();

// it's the same single instance

uni === uni2; // true

// all prototype properties work

// no matter when they were defined

uni.nothing && uni.everything && uni2.nothing && uni2.everything; // true

// the normal properties work

uni.bang; // "Big"

// the constructor points correctly

uni.constructor === Universe; // true
Alternative solution
var Universe;

(function () {

    var instance;

    Universe = function Universe() {

        if (instance) {

            return instance;

        }

        instance = this;

        // all the functionality

        this.start_time = 0;

        this.bang = "Big";

    };

}());
 

References: 

JavaScript Patterns - by Stoyan Stefanov (O`Reilly)

JavaScript Patterns 7.1 Singleton的更多相关文章

  1. JavaScript Patterns 6.7 Borrowing Methods

    Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...

  2. JavaScript Patterns 6.6 Mix-ins

    Loop through arguments and copy every property of every object passed to the function. And the resul ...

  3. JavaScript Patterns 6.5 Inheritance by Copying Properties

    Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...

  4. JavaScript Patterns 6.4 Prototypal Inheritance

    No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...

  5. JavaScript Patterns 6.3 Klass

    Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...

  6. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

    // the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...

  7. JavaScript Patterns 6.1 Classical Versus Modern Inheritance Patterns

    In Java you could do something like: Person adam = new Person(); In JavaScript you would do: var ada ...

  8. JavaScript Patterns 5.9 method() Method

    Advantage Avoid re-created instance method to this inside of the constructor. method() implementatio ...

  9. JavaScript Patterns 5.8 Chaining Pattern

    Chaining Pattern - Call methods on an object one after the other without assigning the return values ...

随机推荐

  1. 用C语言来分割字符串

    #include <stdio.h> int main() { ] = {}; ] = {}; ] = {}; sscanf("1,2,3#3,4#4,5"," ...

  2. Delphi 收藏

    日前整理仓库,翻出了一些 Delphi 产品,以前购买的 Delphi 都有实体产品,包含说明书.光碟片.还有一些广告文宣,而且相当厚实,版本的演进,从外包装也能感受到,到目前的 XE5 版,只剩一个 ...

  3. 搭建java环境

    操作系统:Win10 java版本:1.8.0_102 1.下载安装java JDK(安装时把jre放在了同级目录) 地址为:F:\Program Files\Java 目录下 2.配置环境变量 此电 ...

  4. 2016 年青岛网络赛---Family View(AC自动机)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5880 Problem Description Steam is a digital distribut ...

  5. 【thinkphp5】【THINKCMG】标签部分开发(一)

    最近打算开一个自己的CMS后台轮子,框架选择了thinkphp 5 (别问我为什么选择这个框架)然后想边开发边记录一下,方便自己方便他人 进入正题 1.数据库设计 一共三张表 post文章表这个就不贴 ...

  6. php学习5-时间和日期

    如果时间时区不对,使用时间是要先设定时区,使用date_default_timezone_set() 设置新时区 date_default_timezone_set('Asia/Shanghai'); ...

  7. 从源码角度理清memcache缓存服务

    memcache作为缓存服务器,用来提高性能,大部分互联网公司都在使用.   前言    文章的阅读的对象是中高级开发人员.系统架构师. 本篇文章,不是侧重对memcache的基础知识的总结,比如se ...

  8. 编译安装rabbitmq服务端

    有一种方式是:下载rabbitmq-server-generic-unix压缩包,是不用编译的.是已经编译好的源码了 下面介绍编译源码安装   总括: 需要以下步骤:   1.安装erlange.因为 ...

  9. Maven编译jar出现:无法确定 T 的类型参数的异常的原因和处理方案

    出错场景: 代码: public class JsonUtil { private static final Gson gson = new GsonBuilder().setDateFormat(& ...

  10. Linux学习心得之 jnlp的文件和java应用程序安全设置

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 jnlp的文件和java应用程序安全设置 1.前言2. jnlp的文件打开3.java应用程 ...