Skip to content
This repository
tree: 98045e1a4c
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 149 lines (109 sloc) 2.602 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Singleton
Description: restricts object creation for a class to only one instance
*/

var obj = {
  myprop: 'my value'
};

var obj2 = {
  myprop: 'my value'
};
obj === obj2; // false
obj == obj2; // false

var uni = new Universe();
var uni2 = new Universe();
uni === uni2; // true

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

/*** Instance in a Closure ***/

function Universe() {
 
  // the cached instance
  var instance = this;
  
  // proceed as normal
  this.start_time = 0;
  this.bang = "Big";
  
  // rewrite the contructor
  Universe = function () {
    return instance;
  };
}

function Universe() {
  
  // the cached instance
  var instance;
  
  // rewrite the constructor
  Universe = function Universe() {
    return instance;
  };
  
  // carry over the prototype properties
  Universe.prototype = this;
  
  // the instance
  instance = new Universe();
  
  // reset the constructor pointer
  instance.constructor = Universe;
  
  // all the functionality
  instance.start_time = 0;
  instance.bang = "Big";
  
  return instance;
}



// testing
var uni = new Universe();
var uni2 = new Universe();
uni === uni2; // true

// 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

var Universe;

(function () {

  var instance;
  
  Universe = function Universe() {
    
    if (instance) {
      return instance;
    }
    
    instance = this;
    
    // all the functionality
    this.start_time = 0;
    this.bang = "Big";
  };
}());

// reference
// http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/#singletonpatternjavascript
// http://shop.oreilly.com/product/9780596806767.do?sortby=publicationDate
</script>
</body>
</html>
Something went wrong with that request. Please try again.