GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — JavaScript-Design-Patterns/GoF/classic/Creational/Singleton at master · irynaO/JavaScript-Design-Patterns

Expand file tree

/

Singleton

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
 
 
/

Singleton

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
CoffeeScript
CoffeeScript
 
 
ECMAScript
ECMAScript
 
 
TypeScript
TypeScript
 
 
README.md
README.md
 
 
Singleton.dia
Singleton.dia
 
 
Singleton.png
Singleton.png
 
 

README.md

Outline

Synopsis

As a person, I am unique. No one has exactly my DNA.

Problem

In JavaScript, we can create several instances of everything using:

  • An object literal: e.g. {}
  • The static method "create" of "Object": e.g. Object.create(Object.prototype)
  • The "new" operator on a constructor function: e.g. new Object()

Therefore, I cannot be sure that there will not be other instances of myself in the code!

Solution

Singleton is the key! To implement this pattern, we can:

  • Create a "class" with a private constructor
  • Create a static attribute that will contain the unique instance of our class
  • Create a static method that will check if the instance is unique and return it

Singleton (classic)

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.