How to rewrite classes using closures in JavaScript
gaurangtandon.com
How to rewrite classes using closures in JavaScript
1–10 of 58 posts
Re: How to rewrite classes using closures in JavaScript
#2Thanks for that (and no thanks for this)! You won't find too many this's (or deeply nested properties of any kind) in my own decade+ long adventure with JavaScript.
Will bookmark for future reference.
Re: How to rewrite classes using closures in JavaScript
#3Thanks for that (and no thanks for this )! You won't find too many this's (or deeply nested properties of any kind) in my own decade+ long adventure with JavaScript. Will bookmark for future reference.
Glad you liked it :)
Re: How to rewrite classes using closures in JavaScript
#4please dont do this in any codebase other people are working on
Re: How to rewrite classes using closures in JavaScript
#5It’s especially nice because now every instance has to carry around every copy of every method, type testing is impossible, and if you’re lucky you’re even making every callsite megamorphic and defeating all those pesky optimisations we all hate.
Re: How to rewrite classes using closures in JavaScript
#6The good old let-over-lambda http://www.csci.viu.ca/~wesselsd/courses/csci330/slides/lol.... . Now, in JS AFAIK this causes high memory usage, as "methods" are separate for each instance.
Re: How to rewrite classes using closures in JavaScript
#7The good old let-over-lambda http://www.csci.viu.ca/~wesselsd/courses/csci330/slides/lol.... . Now, in JS AFAIK this causes high memory usage, as "methods" are separate for each instance.
Love that book. Particularly the fact that the method-to-function mapping doesn't have to be one-to-one. You can always implement multiple methods in a single function (and for prototyping with closures this is usually a good idea).
Re: How to rewrite classes using closures in JavaScript
#8I avoid classes, but 'this' is not too horrible if you deconstruct the objects/methods/variables you need from it. 'const {foo, baz}=this;'
Re: How to rewrite classes using closures in JavaScript
#9Except that classes are more memory efficient and better optimized in the VM.
This will package a copy of the functions with every instance you make, the class variant will only create member variables for each instance.
Maybe save a few bytes down the wire with a different developer experience for fundamentally more bloated code.
Re: How to rewrite classes using closures in JavaScript
#10Funny thing is that a lot of developers like that JavaScript received class support, and makes it easier to write oop.
Many people dient understand 'this' in JavaScript, and ways are created to reduce the usage of 'this'.
And now people are switching from classes, a feature many want before and complained it was not in JavaScript.
I think we always have to accept the language how it is, and dont find weird ways to do things the language is not made for. (like e.g. typescript)