DEV Community

Cover image for Introducing Metho: Safely adding superpowers to JS

Introducing Metho: Safely adding superpowers to JS

Jon Randy 🎖️ on October 12, 2021

TL;DR Metho allows you to easily and safely add methods in the form of dynamic properties to any object. Sounds boring, but if used to e...
Collapse
 
guitarino profile image
Kirill Shestakov

How is 2[to(13)] better than range(2, 13)? Or 13[isEven] better than isEven(13)?

I see how it's an interesting syntax trickery to play around with, but I don't see how that produces anything other than confusion. It's definitely not something I'd want to see in production.

Collapse
 
lawrencedol profile image
Lawrence Dol

Completely agree. Interesting, but of no pragmatic value beyond raising an interesting idea.

Collapse
 
codingjlu profile image
codingjlu

Come on, the syntax looks cool. No, practicality isn't the point here.

Collapse
 
nombrekeff profile image
Keff

Interesting concept I must say. Quite elegant! I don't think I would use it in a real scenario, as it's not so clear how it works and would confuse people initially. Though it's fascinating, and might use it some time in one of my projects!

Collapse
 
lawrencedol profile image
Lawrence Dol • Edited

Agree that it is interesting; but it's not elegant. It's confusing, abstruse, inefficient, and has no upside over writing a simple function.

Collapse
 
iamandrewluca profile image
Andrei L

This is DOPE! I'm laughing so hard ))
These are JavaScript Extensions, like Swift and Kotlin extensions

Collapse
 
tracygjg profile image
Tracy Gilmore

The development and discussion of extensions like this (for any language) is a great way to test the viability of new features before preparing formal proposals.
They might end up just being something the original developer finds useful or merely interesting, but others might also find them of use.
They should carry a health warning until they are fully tested, adequately documented and licenced. But then there are far too many widely used packages (JS and other stacks) that fall short of this stipulation.
Just look at how John Resig's jQuery influenced the evolution of JS and the development of Web APIs and continues to be in wide usage.

Collapse
 
aminmansuri profile image
hidden_dude • Edited

Looks like Smalltalk to me.

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Really? Haha... I've read about Smalltalk, but have never seen any code

Collapse
 
aminmansuri profile image
hidden_dude

This is what Smalltalk looks like:

1 to: 5 do: [ x | Transcript show: x]

That would "print" the numbers from 1 to 5.
Smalltalk is both OO and functional. It receives the method to:do: and you pass it a "block" (known as a "lambda" or "closure" in lesser languages) as [ ].

My theorem of languages states:

"Smalltalk was too advanced for it's time, and the last of the truly innovative languages. Most people don't understand it, but have spent the last 40 years trying to create languages that are more like it but utterly fail because they fail to adopt it's simple and intuitive syntax and the full breadth of it's features"

C++ added some OO-like features, but wasn't enough.
So Java added VMs and GC and some introspection that brought it closer to Smalltalk.
Ruby went further and added metaclasses and some of the other features.
Now JavaScript has suddenly become functional (and has "added" classes) but keeps getting messier and messier.

Maybe one day people will embrace the simple brilliant elegance of Smalltalk and put down these other messy and incomplete tools. But I'm not holding my breath.. more likely we'll end up with some lame imitation that is messy.

Thread Thread
 
lawrencedol profile image
Lawrence Dol

JS hasn't "become" functional, at all -- it was functional from it's inception. Even it's prototypical OO is functional. The function truly is, and has always been, the core abstraction of JS.

What I wish is that JS engines would recognize this and deeply optimize function closures to remove any penalty for using them as object instances.

Thread Thread
 
aminmansuri profile image
hidden_dude

Point taken.. but still a messy language when compared to Smalltalk.

Collapse
 
activenode profile image
David Lorenz

Nice idea!

Collapse
 
merthod profile image
Merthod

Just a question, what about TypeScript?

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Not a fan of TypeScript at all, but according to some who've looked at this idea with relation to TS - it apparently doesn't play very well.

Collapse
 
jonrandy profile image
Jon Randy 🎖️

If you want to have a go at making it integrate well with TS though, please go right ahead! Contributions welcome

Collapse
 
superwibr profile image
superwibr

You thought of it before I could! I am currently building a library that would use symbols as semi-private methods. I must say, this is quite better than whatever I was thinking :)

Collapse
 
ignacionr profile image
Ignacio Rodríguez

You keep creating things of beauty. This one seems to experiment on the connection between data and behaviour, exposing that there are more elegant ways than what was given to us as OOP.

Collapse
 
aminmansuri profile image
hidden_dude • Edited

and yet he's recreating Smalltalk syntax (and Smalltalk is the definition of what "real" OOP is)

He's using an OO language (JavaScript) with Functional style to implement another OO language's syntax

Collapse
 
codingjlu profile image
codingjlu

Quite interesting, but I can't at all see the proper motivation or benefits.

Collapse
 
polterguy profile image
Thomas Hansen

Hahahahaha :D