Re:New-less JavaScript (2013)
by jfirebaugh on Thursday March 12, 2015 @02:14AM
(#9189401)
(author here)
Absolutely. My preference for new modules is to use both newless and "this"-less Javascript. Newless via the technique outlined in this post (or encapsulated in a module: https://github.com/Mr0grog/newless), and this-less via the module pattern (see link in post).
This is the pattern followed by D3. It's easy to follow internally, gives you true private data and methods, and (IMHO) is a nicer interface for API consumers -- no `new`, `delete`, or `bind`'s necessary.
With ES6 classes everywhere soon , I doubt you'll see less "new". On the contrary, you'll see more java-ish code and less Doug Crockford like code. By the way, he aint in TC39 anymore. So expect even more java-ish features in ES.
Re:New-less JavaScript (2013)
by AnkhMorporkian on Thursday March 12, 2015 @01:47AM
(#9189345)
I don't think there's anything inherently evil, bad, or even particularly confusing about this. As long as you understand how it's being attached to a running function there's very little confusion to be had. The problem is that most of the resources for learning completely gloss over this incredibly important aspect of the language.
...and too few of the posts complaining about it mention strict mode or ES6 fat arrow syntax.
The problem with module pattern which enables you to use "this"-less Javascript is increased memory usage. Of course you can use other techniques for elements which you instaniate in thousands, but then you are mixing one pattern with another and it will begin hard to follow which should be which. I prefer consistency here.
Re:New-less JavaScript (2013)
by jsprogrammer on Thursday March 12, 2015 @03:39AM
(#9189528)
In theory, an optimizing VM/JIT should be able to handle this for you. I'm not sure if existing implementations currently do any optimizations though.
Re:New-less JavaScript (2013)
by jsprogrammer on Thursday March 12, 2015 @02:09AM
(#9189390)
Agreed, `new` and `this` are pretty much useless boilerplate noise. Gimme a closure and watch me wipe out a quarter of the text in your source and never even have to think about using a`bind`.
Yes, absolutely. In fact, this post is really just providing a trick for not having to always write `new`. Using closures for object construction is the real 'new-less' JavaScript. As a nice parallel, the trick explained in the post lets you construct objects using `new` without typing it, whereas object factories that use closures allow you to use `new` without requiring it.
So many headaches are to be had if you're not careful and using some decent functional things.