Last active
August 29, 2015 13:57
-
-
Save user24/9736172 to your computer and use it in GitHub Desktop.
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
// 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 |
@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
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.