Created
June 22, 2011 21:25
-
-
Save insin/1041256 to your computer and use it in GitHub Desktop.
sillySingleton
This file contains hidden or 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
| // Please... just don't | |
| var S = (function() { | |
| var instance = null | |
| function S() { | |
| if (instance !== null) return instance | |
| if (!(this instanceof S)) return new S() | |
| instance = this | |
| } | |
| S.getInstance = function() { | |
| return instance !== null ? instance : new S() | |
| } | |
| return S | |
| })() |
This file contains hidden or 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 PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> | |
| <html lang="en"> | |
| <head> | |
| <title>sillySingleton QUnit Tests</title> | |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
| <meta name="Author" content="Jonathan Buchanan"> | |
| <!-- Test framework --> | |
| <link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen"> | |
| <script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script> | |
| <!-- Code under test --> | |
| <script type="text/javascript" src="sillySingleton.js"></script> | |
| <!-- Test cases --> | |
| <script type="text/javascript" src="tests.js"></script> | |
| </head> | |
| <body> | |
| <h1 id="qunit-header">sillySingleton QUnit Tests</h1> | |
| <h2 id="qunit-banner"></h2> | |
| <div id="qunit-testrunner-toolbar"></div> | |
| <h2 id="qunit-userAgent"></h2> | |
| <ol id="qunit-tests"></ol> | |
| <div id="qunit-fixture"></div> | |
| </body> | |
| </html> |
This file contains hidden or 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
| QUnit.module('sillySingleton'); | |
| QUnit.test('sillySingleton', 4, function() { | |
| var instance = S() | |
| ok(instance instanceof S, 'Calling the constructor correctly creates an instance') | |
| ok(new S() === instance, 'Creating with new returns the same instance') | |
| ok(S() === instance, 'Calling the constructor returns the same instance') | |
| ok(S.getInstance() === instance, 'Calling getInstance returns the same instance') | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment