Тёмный

Adding Method Overloading To Classes In JavaScript? 

Low Byte Productions
Подписаться 78 тыс.
Просмотров 3,9 тыс.
50% 1

Опубликовано:

 

21 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 26   
@BryanChance
@BryanChance 2 года назад
I have a special playlist specifically for this channel. :-) The content is so much more than about Javascript.
@taskforce_kerim
@taskforce_kerim 3 года назад
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.
@LowByteProductions
@LowByteProductions 3 года назад
Ah operator overloading would be great. And thank you - it's much appreciated!
@Kenbomp
@Kenbomp 3 года назад
Operator overload is possible in smalltalk. Keyword parameters help quite a bit. You can a actually define your own operators such as+++ though this has it's trade offs
@AbhinavKulshreshtha
@AbhinavKulshreshtha 3 года назад
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.
@LowByteProductions
@LowByteProductions 3 года назад
Yeah I see where you're coming from. I think overloading is like many other kinds of language feature in that it can be used in some really counter productive ways.
@MMetalRain
@MMetalRain 3 года назад
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!
@LowByteProductions
@LowByteProductions 3 года назад
Thanks Otto - I've played around with this kind of thing before. If you're parsing the function strings yourself it can get quite hairy now that JS allows default values to be assigned. Still, if you expect the users to "play by the rules", and you don't care about modeling more complex data types, you could probably get it to work quite nicely! One of the most insane projects I've ever seen doing this kind of thing is ganja js (github.com/enkimute/ganja.js/). It parses function strings and allows for operator overloading, repurposing the scientific notation for complex numbers, and a bunch of other crazy stuff.
@Vorticalbox
@Vorticalbox Год назад
Could I please know what theme you're using? It looks awesome
@LowByteProductions
@LowByteProductions Год назад
Dracula
@jussinevavuori2598
@jussinevavuori2598 3 года назад
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(...) {...} }, { ... } ]) }
@jussinevavuori2598
@jussinevavuori2598 3 года назад
Would probably be considerably easier to implement however would not be as useful, teaching and entertaining to watch as this video was!
@LowByteProductions
@LowByteProductions 3 года назад
Thanks Jussi. I agree that the syntax is not ideal. The trouble with the method you propose is that the methods will be per instance rather than per class - so if you have 1000 instances you'll have the same number of copies of the method in memory.
@aarond309
@aarond309 2 года назад
why not use a combination of arguments.length and the typeof or instanceof operators to dynamically run the commands necessary for each respective situation?
@antares-the-one
@antares-the-one 11 месяцев назад
create a function with such implementation and benchmark it. The difference reaches tens of times
@IlyasFasikhov
@IlyasFasikhov Год назад
Why not use just reflect or proxy to achieve that?
@antares-the-one
@antares-the-one 11 месяцев назад
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...
@scragar
@scragar 2 года назад
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).
@DanielTateNZ
@DanielTateNZ 3 года назад
Very cool stuff :)
@Kenbomp
@Kenbomp 3 года назад
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
@Captain__Obvious
@Captain__Obvious 3 года назад
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...
@LowByteProductions
@LowByteProductions 3 года назад
Oh yeah for sure - in a bunch of ways. That's why I clearly mention in the video that it shouldn't be used for performance critical code.
@erikitter6773
@erikitter6773 2 года назад
the horrors of interpreted languages combined with the horrors of languages without proper type systems
@phasm42
@phasm42 3 года назад
Typescript is what you're looking for.
@LowByteProductions
@LowByteProductions 3 года назад
This is for projects where TS isn't an option.
@marcellerusu
@marcellerusu 2 года назад
Typescript doesn’t address what this video is about Typescript explicitly avoids extending the runtime type system which is required for dynamic dispatch