News for hackers. Stuff that matters.
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.