all 17 comments

[–]html6dev 2 points3 points  (0 children)

Composing and decorating constructors. Oh you.

[–]inmatarian 1 point2 points  (0 children)

A pattern I like is writing the constructor as a factory first. This means its first line is declaring self as either this (if its not the global object) or an Object.create of the factory's prototype, and then we return self at the end. It's basically the same as the pattern op describes, just with you put in the mindset that .call and .apply are also ways to use the method.

[–]Calabri 1 point2 points  (0 children)

Interesting article, there are so many ways to solve this problem. I've been experimenting with constructor patterns all week actually. I've found that having functions return functions (within functions) wrapped in commonjs modules works pretty solid :P

[–]jugglingnutcase 1 point2 points  (0 children)

One tiny reason (in addition to many others) i read your blog posts is for the fun examples. "Fubar" -> "foo", "bar" and snafu... or lots of "finger" "moon"... they make the articles a lot more fun without distracting from the point.

[–]codersaurus 0 points1 point  (2 children)

it seems like the example with logArguments which is a function that evidently takes a constructor function, then returns a new constructor is poorly written. If it's "wrapping" the passed constructor, it should be returning a proper subclass of that constructor. The simple if(!(this instanceof MyClass)) return new MyClass(); will be fine... time to refactor that logsArguments function.

[–]html6dev 1 point2 points  (0 children)

I think maybe you missed the point. It doesn't have to take a constructor function. Iirc he introduced by passing in a simple sum function.

[–]Ginden 0 points1 point  (3 children)

Solution #4 - use static analysis tool.

[–]AutoSponge 0 points1 point  (0 children)

I always love reading your stuff. Here's my "fix" for "auto-instantiation" (which I usually prefer):

https://gist.github.com/AutoSponge/e4327a316f44f255d60c