Created
February 17, 2011 03:45
-
-
Save founddrama/830928 to your computer and use it in GitHub Desktop.
Code for "faking it" blog post re: static methods in JavaScript.
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
| // the "usual" way... | |
| // (1a) init your class and assign to a variable: | |
| var myWidget = new net.fd.SomeWidget(); | |
| // now we have our instance | |
| // (1b) call the method from your instance: | |
| myWidget.inspectContext(); | |
| // ...except inspectContext() tells returns false | |
| // meaning that we created myWidget for nothing | |
| // (2) try something a little different | |
| net.fd.SomeWidget.inspectContext(); | |
| // error! - "inspectContext" is undefined | |
| // ...because "net.fd.SomeWidget" is the constructor | |
| // (3) try it again, with a twist | |
| net.fd.SomeWidget.prototype.inspectContext(); | |
| // still returns false but this time we didn't have | |
| // the extra overhead of creating the instance |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@seehttp://blog.founddrama.net/2010/06/faking-it/