Skip to content

Commit 2005137

Browse files
committed
Correcting singleton
1 parent f13f552 commit 2005137

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

Chapter 3/Singleton.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var Westeros;
44
function Wall() {
55
this.height = 0;
66
if (Wall._instance)
7-
throw new Error("This is a singleton, use getInstance to get an instance");
7+
return Wall._instance;
88
Wall._instance = this;
99
}
1010
Wall.prototype.setHeight = function (height) {
@@ -33,3 +33,6 @@ var wall2 = Westeros.Wall.getInstance();
3333
wall2.getStatus();
3434
var wall3 = new Westeros.Wall();
3535
wall3.getStatus();
36+
wall3.setHeight(20);
37+
var wall4 = new Westeros.Wall();
38+
wall4.getStatus();

Chapter 3/Singleton.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Westeros
66
private height:number = 0;
77
constructor(){
88
if(Wall._instance)
9-
throw new Error("This is a singleton, use getInstance to get an instance");
9+
return Wall._instance;
1010
Wall._instance = this;
1111
}
1212
public setHeight(height:number){
@@ -34,3 +34,6 @@ var wall2 = Westeros.Wall.getInstance();
3434
wall2.getStatus();
3535
var wall3 = new Westeros.Wall();
3636
wall3.getStatus();
37+
wall3.setHeight(20);
38+
var wall4 = new Westeros.Wall();
39+
wall4.getStatus();

0 commit comments

Comments
 (0)