Echo JS 0.11.0

<~>
jeromegn 4194 days ago. link parent 1 point
`Object.create` or `model.call` both feel so weird though! It must be my OO reflexes bubbling up.

What's wrong with using `new model` instead of doing `model.call`? I understand you lose the scope of `this`, but I'm pretty sure there are ways around that.

I wrote a library that uses that syntax for inheritance. You'll probably find it's ludicrous ;) http://github.com/jeromegn/mongol (my `Model` in there is a factory that spits out constructors...)

Interesting talk! Hope to hear more from you soon.

Replies

ericelliott 4193 days ago. link 3 points
They do feel awkward, don't they? First of all, when you're using functional inheritance (the one that requires *either* .call, or to have you pass the object in as an argument), you're specifically modifying an object that *already exists*, and since `new` doesn't let you change the value of `this`, and implies that you're creating a new object, that seems silly and counter to the semantics of what you're actually trying to accomplish. Think of functional inheritance is mixing in private state and privileged methods *after* an object has already been instantiated.

Second - I *always* do that work inside a factory... so I never actually expose that .call or object-pass-in requirement to end users of the API. I assume all the awkwardness, and if you don't want to deal with the awkwardness yourself, you can check out libraries like Stampit, where it's handled for you by `.enclose()`. https://github.com/dilvie/stampit

I believe I've taken a look at mongol before. I certainly like some of the features. That said, I have pretty strong feelings about requiring people to use `new`. It forces all your callers to be tightly coupled to your chosen method of object instantiation, making it harder for you to switch to true factory patterns if you want to add that flexibility later. By requiring callers to use `new`, you're forcing them to program to your *implementation* of object instantiation, breaking one of the golden rules of OO design: "program to an interface, not an implementation".

It's also a violation of the open/closed principle. See http://ericleads.com/2013/01/javascript-constructor-functions-vs-factory-functions/ and http://ericleads.com/2012/09/stop-using-constructor-functions-in-javascript/

- Eric