Skip to content

Instantly share code, notes, and snippets.

@founddrama
Created February 17, 2011 03:45
Show Gist options
  • Select an option

  • Save founddrama/830928 to your computer and use it in GitHub Desktop.

Select an option

Save founddrama/830928 to your computer and use it in GitHub Desktop.
Code for "faking it" blog post re: static methods in JavaScript.
// 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
@founddrama
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment