单例(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 create the first time.单例模式是指在程序中一种特殊的类(class),他们仅有一个实例。这意味着当这个类在被第一次实例化后,之后的实例化操作都将获得到第一次实例化时产生的实例。
对象字面量与单例
以对象字面量(Object Literal)定义的对象本身即为单例:
|
|
对象字面量实现单例模式的另外一种形式: