Skip to content

Instantly share code, notes, and snippets.

@user24
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save user24/9736172 to your computer and use it in GitHub Desktop.
Save user24/9736172 to your computer and use it in GitHub Desktop.
// A way to use module pattern along with new keyword
function Foo() {
this.baz = 4;
function bar() {
return this.baz;
}
return {
"bar":bar.bind(this)
};
}
var f = new Foo();
f.bar(); // 4
@user24
Copy link
Author

user24 commented Mar 24, 2014

So, yes you could just mess about with Foo.prototype.bar = (....) but I don't like that syntax, and the result is the same using this much more familiar pattern. It also makes it much easier to change to a standard Revealing Module later down the line if need be.

@user24
Copy link
Author

user24 commented Mar 24, 2014

@jamesallardice just pointed out that this won't return an instance, so f instanceof Foo is false >:(

https://twitter.com/james_allardice/status/448013229143629824

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