Adding Method Overloading To Classes In JavaScript?

preview_player
Показать описание
In this video we're building a runtime library that allows us to define more powerful classes. More specifically, it'll give us the ability to write strongly typed and overloadable constructors and methods.

=[ 🔗 Links 🔗 ]=

Рекомендации по теме
Комментарии
Автор

I have a special playlist specifically for this channel. :-) The content is so much more than about Javascript.

BryanChance
Автор

Love the video and your consistently amazing content. It's a shame that there aren't more videos about even slightly advanced programming topics. Thank you for your great work.

I am not a huge fan of method overloading, but I really really miss operator overloading. Being forced to use method chaining when using Big.js, for example, just feels kind of clumsy.

taskforce_kerim
Автор

Amazing content .. personally I prefer not to overload if possible but it is an important principal of oops. My worst (or best) work was writing 15 overloaded methods for a java package in a company I worked for. If I have to do that again, i would prefer named parameters in kotlin. Named parameters in kotlin and now php8 is amazing for overloading methods without duplicating efforts for programmers.

AbhinavKulshreshtha
Автор

I was thinking if there was some good way of annotating existing functions with types, without having all that extra machinery like:
function add(
a /* number */,
b /* number */) {
..
}
and then using Function.prototype.toString to extract those annotations, but that extra string -> type conversion will be quite hard to manage as well.

Great video as always!

MMetalRain
Автор

Great video! My biggest problem here is the API: you're inventing class syntax again. I would love to use existing classes but for some methods (static or not) just define them like you do:

class Vector3 {
constructor(...) {...}

add = overload((V3, V3Type) => [
{
args: [...],
fn: function(...) {...}
},
{ ... }
])
}

jussinevavuori
Автор

Could I please know what theme you're using? It looks awesome

Vorticalbox
Автор

this is the reason webpages are so stupidly slow in relation to the useful computations needed to perform some actions. Just to be able to pass different arguments to the function we execute 300 lines of code, some of which in 2 external libraries...

antares-the-one
Автор

Why not use just reflect or proxy to achieve that?

IlyasFasikhov
Автор

Method overload doesn't buy you much in clarity of behavior. Parameters were never meant for inheritance abuse. But at least this is addressed in the video

Kenbomp
Автор

why not use a combination of arguments.length and the typeof or instanceof operators to dynamically run the commands necessary for each respective situation?

aarond
Автор

Kind of annoys me that you collapse the argauments into methodArgs specifically so you can expand then re-collapse them immediately afterwards. Passing around an array of arguments would have been cleaner, especially when JS provides a magic variable for that(arguments).

scragar
Автор

Cool stuff but unfortunately this approach is one big V8 deopt in terms of performance. It won't play nice with hidden classes and inline caching (you could test with V8 Indicium). And an O(N) loop every method call isn't great, though imo the best approach is not paying any runtime implementation cost to begin with...

Captain__Obvious
Автор

Typescript is what you're looking for.

phasm
Автор

the horrors of interpreted languages combined with the horrors of languages without proper type systems

erikitter