Created
November 5, 2013 18:37
-
-
Save j127/7323822 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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