Re:Treating JavaScript like a 30 year old language
by AjJi on Wednesday July 04, 2012 @11:55AM
(#4199829)
AFAIK, using 'new' is not a matter of preference. Omitting the keyword leads to different results. Am I missing something?
[ Parent
| on HN ]
Re:Treating JavaScript like a 30 year old language
by MatthewPhillips on Wednesday July 04, 2012 @12:00PM
(#4199848)
Some people think that library consumers shouldn't have to use new. Instead they should call a factory function (which itself calls new).
Re:Treating JavaScript like a 30 year old language
by phleet on Wednesday July 04, 2012 @12:09PM
(#4199884)
Here's an argument in favor of not requiring library consumers to use new by the author of the Raphäel library: http://dmitry.baranovskiy.com/post/something-new
Re:Treating JavaScript like a 30 year old language
by orangecat on Wednesday July 04, 2012 @01:08PM
(#4200114)
And I agree with that. "new" exposes too much implementation detail: it always allocates an object, and it's exactly the type specified, neither of which callers should normally care about. I much prefer how Python does it.
Re:Treating JavaScript like a 30 year old language
by ufo on Wednesday July 04, 2012 @04:26PM
(#4200755)
Actually, in Javascript you can preemptively return a value of a different type from the constructor instead of relying on the implicit `return this` at the end.
That said, I agree that the way "new" specifies the concrete type of the returned value is pure evil. In OO things should be typed according to their interfaces, not according to their concrete implementations.
Re:Treating JavaScript like a 30 year old language
by petercooper on Thursday July 05, 2012 @09:31PM
(#4205941)
It does but there are other ways to create objects (even using prototypal inheritance) in JavaScript that are pretty popular in the wild (Object.create and object literals with {} for example). The code behind this and the constructors, etc, would need to be rather different though (that is, you're right, you can't just drop new and expect it to work as-is.)
Re:Treating JavaScript like a 30 year old language
by shawndumas on Wednesday July 04, 2012 @01:09PM
(#4200118)
if (!(this instanceof arguments.callee)) {
return new arguments.callee(/* parms */);
}
Re:Treating JavaScript like a 30 year old language
by ufo on Wednesday July 04, 2012 @04:28PM
(#4200763)
You can't use arguments.callee when in strict mode. So far the only alternative is to manually encode the class name and argument list:
if(!(this instanceof MyClass)){
return new MyClass(a,b,c)
}
Re:Treating JavaScript like a 30 year old language
by shawndumas on Wednesday July 04, 2012 @08:23PM
(#4201264)
Correct. But, like John Resig, I am gonna miss it...
"Personally, I’m going to miss arguments.callee,
I used it in a number of places in my code."
-- John Resig (May 22, 2009 at 7:26 am) [1]
----[1]: http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more...