Skip to content

Instantly share code, notes, and snippets.

@j127
Created November 5, 2013 18:37
Show Gist options
  • Save j127/7323822 to your computer and use it in GitHub Desktop.
Save j127/7323822 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>JS Design Patterns</title>
<meta charset="utf-8" />
</head>
<body>
<h1>Singleton Pattern</h1>
<button id="createWorld">Create a World</button> <button id="createAnotherWorld">Create Another World</button>
<div id="output"></div>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function() {
$('#createWorld').on('click', function() {
var world = new World();
console.log(world);
});
$('#createAnotherWorld').on('click', function() {
var anotherWorld = new World();
console.log(anotherWorld);
});
});
function World() {
// saved instance
var instance = this;
this.name = "I'm unique";
var d = new Date();
this.createdTimeStamp = d.getTime();
World = function() {
return instance;
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment