17

One of my favourite things in JS is the absolutely cursed shit that it lets me do.

Did you know that you can return anonymous classes from functions?

Comments
  • 4
    Yes. It can also monkey patch classes and add methods to Object instances.

    Why, you ask?
    Because JS.
  • 5
    may I introduce you to the church of closures

    https://en.wikipedia.org/wiki/...
  • 9
    I really don't see the point programming it like this. But anyway, this could be seen as a factory
  • 1
    I mean, classes are just objects...
  • 3
    @kiki I'm already a functional programmer by heart. I eat closures for every meal of the day
  • 4
    @retoor a factory that when an order comes in, a new machine to create the product is automatically generated and sent to the customer.

    It’s a factory factory.
  • 5
    By the way OP if you tried to put some horseshit like this in my code base your review would be denied and I would also fucking kill you.
  • 2
    I’m ready to puke
  • 1
    Ha! Cool. Never thought to do something like that.
  • 4
    Javascript is one of the most cursed languages out there.

    And I am here for it
  • 3
    @AlgoRythm if he were to put some shit like this in my codebase I would buy the man a beer.

    He has seen enough, the void called to him and he is trying to make others listen.
  • 2
    @AleCx04 he is the void. I’d buy him ten beers so he can’t operate a keyboard!
  • 0
    It's not exclusive to JS. It's allowed on dart/flutter too since it's actually useful sometimes. You can treat anonymous function like variable, by passing them to other widget/method, or returning them.
  • 1
    @AleCx04 there are some use cases. FP higher order function. This example returns a function whose prototype is set to an anonymous class. Before React Hooks HOC's "higher order components" were all the hype, those used a similar mechanism
  • 1
    @webketje It can also be used to implement mixins by creating locally scoped class definitions from which you only "inherit" a subset of methods
  • 0
    js developer discovers factory pattern
  • 1
    @AleCx04 the void has cookies and they are delicious
Add Comment