Live data from Hacker News

How to rewrite classes using closures in JavaScript

gaurangtandon.com

1–10 of 58 posts

Re: How to rewrite classes using closures in JavaScript

#7
post #6

The 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

#9
Except 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

#10
Funny 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)
Post reply on HN