4 ms·
I think a good complement tothis concept is that of "this"-less Javascript, relegating it only to things like mixins. So many headaches are to be had if you're
by rtpg 12y ago
I think a good complement tothis concept is that of "this"-less Javascript, relegating it only to things like mixins.
So many headaches are to be had if you're not careful and using some decent functional things.
- AnkhMorporkian 12y agoI 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.
- insin 12y ago...and too few of the posts complaining about it mention strict mode or ES6 fat arrow syntax.
- jsprogrammer 12y agoAgreed, `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`.
- bttmly 12y agoYes, 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.
- jfirebaugh 12y ago(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 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.
- aikah 12y agoWith 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.
- methyl 12y agoThe 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.
- jsprogrammer 12y agoIn 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.