16.09.2014, 19:13, "Rick Waldron" <waldron.r...@gmail.com>:
On Tue, Sep 16, 2014 at 11:10 AM, Alex Kocharin <a...@kocharin.ru> wrote:16.09.2014, 18:56, "Rick Waldron" <waldron.r...@gmail.com>:On Mon, Sep 15, 2014 at 8:37 PM, Alex Kocharin <a...@kocharin.ru> wrote:15.09.2014, 23:23, "Rick Waldron" <waldron.r...@gmail.com>:On Mon, Sep 15, 2014 at 2:57 PM, Brendan Eich <bren...@mozilla.org> wrote:Rick Waldron wrote:The first is also objectionable because it breaks existing implicit return semantics.Say what? Constructors can return a different object from `this`, that's just JS.Yikes, I should've been more specific. Generally, it's considered an anti-pattern to write constructors that explicitly return an object to override `this`(for all the reasons you'd expect)—that's not to say that it isn't done or doesn't exist (and certainly I didn't mean to imply that it wasn't possible). Design that might rely on that pattern would conflict with widely accepted best practices.Writing constructors that override `this` is not an anti-pattern. It's an implementation detail that should not ever matter to any outside code.If your code breaks because somebody returns another object from constructor, you're doing it wrong.Overriding `this` with an explicit return object will break the link to the constructor's prototype object properties:function C() {return {};}C.prototype.m = function() {return "Previously on Lost";};var c = new C();console.log(c.m()); // nope.This only proves that your example has a bug in it, and correct code should look like this:This contradicts your previous argument:It's an implementation detail that should not ever matter to any outside code.
My argument was that this:
function C() { if (!(this instanceof C)) return new C(); }
And this:
function C() { return Object.create(C.prototype); }
Those implementations are equivalent, because you can run them with "let x = new C()" and expect "x.m()" to work.
You can freely switch between them, and nothing bad should happen. That's what I call implementation detail that should never matter to code using the constructor.
It might be a matter of personal preference or code style, but I don't see an anti-pattern anywhere.
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss