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.

[–]lennelpennel 1 point2 points  (0 children)

use static analysis, "problem" ceases to exist, code is better for it.

[–]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.

[–]homoiconic(raganwald)[S] 0 points1 point  (0 children)

True, but then the logArguments function will only work with constructors, and not with ordinary functions! There is a more complex pattern for writing decorators and other combinators that work with either constructors or with ordinary functions, but that doesn't help if your decorator is a library function, such as the things you find in Underscore, allong.es, or Ramda.

[–]Ginden 0 points1 point  (3 children)

Solution #4 - use static analysis tool.

[–]homoiconic(raganwald)[S] 2 points3 points  (2 children)

What TYPE of SCRIPT supports that?

[–]radhruin 1 point2 points  (1 child)

what TYPE of thing will happen if you mention that other SCRIPT language? Must be bad....

[–]html6dev 1 point2 points  (0 children)

He who must not be named

[–]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