News for hackers. Stuff that matters.
Re:Node.js Best Practices by Touche on Friday September 19, 2014 @05:27PM (#8342588)
Agree about the instanceof trick. I hate that pattern. I'm not sure if it's done to avoid using "new" or because people legitimately make the mistake.
Re:Node.js Best Practices by jkrems on Friday September 19, 2014 @10:21PM (#8343472)
It's not about "making a mistake", it's about cases where you'd want to create objects but using `new` would be awkward. E.g. `myListOfRawData.map(MyConstructor)` when you unmarshal data. Sure, you could create an anonymous function that calls new. But it's easier to create higher level functions and tools when the new is optional.
Re:Node.js Best Practices by jwmerrill on Friday September 19, 2014 @05:31PM (#8342599)
It's to avoid using "new".
Re:Node.js Best Practices by aikah on Friday September 19, 2014 @05:54PM (#8342704)
> It's to avoid using "new".

yeah,well one is still using new inside the constructor.

it's pretty obvious to me that :

1/ one should read the docs before using an api

2/ capitalized functions are meant to be used as constructors.

3/ If you dont like this pattern,write builders and make them obvious. like Object.create()

To me when i design js APIs, I have 2 kind of "templates" :

the jQuery "$(o).after(b).get(0)"/underscore "_(object).method().value()" monadic api style that wraps types (like promises)

OR

The way the DOM is built,more java-ish with builders everywhere ( document.createElement ... ).

So I hardly use new anymore.

I get that people might make mistake or like Python style OO;but still, I think forcing the "new" in unecessary.

Re:Node.js Best Practices by iLoch on Friday September 19, 2014 @05:55PM (#8342712)
Cant you just "return this;" to avoid using "new"?
Re:Node.js Best Practices by aikah on Friday September 19, 2014 @07:41PM (#8343071)
> Cant you just "return this;" to avoid using "new"?

THIS will be the global scope if a function meant to be a constructor is called without new.

Try that in your console

   function Foo(){this.foo = "bar";return this}
then do :

   Foo() ;
it will return window;

so the answer is no.

Re:Node.js Best Practices by greg5green on Friday September 19, 2014 @07:58PM (#8343126)
The answer is yes if you do:

    Foo.call({});
Re:Node.js Best Practices by LazerBear on Friday September 19, 2014 @08:12PM (#8343161)
That's a cool trick, though 'instanceof' won't work in this case.
Re:Node.js Best Practices by cygx on Friday September 19, 2014 @08:21PM (#8343190)
Yup. The proper way to emulate new is

    Foo.call(Object.create(Foo.prototype))
Re:Node.js Best Practices by LazerBear on Friday September 19, 2014 @08:50PM (#8343266)
Douglas Crockford, is that you? Oh the lengths people will go to avoid using 'new'...

I wonder if any linters out there warn when they see something like:

  something = SomeCapitalizedFunction()
Because forgetting to use 'new' is really the only thing I could think of that makes this pattern dangerous.
Re:Node.js Best Practices by jdlshore on Saturday September 20, 2014 @12:30AM (#8343679)
Yes, jslint and jshint both do, by default, iirc. They'll also warn on the opposite (using 'new' with a function that doesn't start with an uppercase letter).
Re:Node.js Best Practices by LazerBear on Sunday September 21, 2014 @12:58PM (#8347594)
Ah that's fantastic, i've been using them for a while and never noticed.
Re:Node.js Best Practices by hderms on Saturday September 20, 2014 @04:59PM (#8345574)
why do people want to avoid using new?
Re:Node.js Best Practices by LazerBear on Sunday September 21, 2014 @01:02PM (#8347608)
If you accidentally forget to use 'new' when you're supposed to, the "constructor" method will change the global object instead of a new object, it can cause strange behavior that is very hard to trace.
Re:Node.js Best Practices by hderms on Friday October 03, 2014 @01:58PM (#8406689)
Ah, I see. I guess I've never forgotten to use new. That does sound incredibly annoying to debug.