FUNCTION OVERLOADING IN JAVASCRIPT

preview_player
Показать описание
JavaScript may not support Function Overloading, but we can simulate the behavior.

In other languages, an overloaded function can be created by adding another function with the same name. This can't be done in JavaScript because the 2nd function will overwrite the 1st function.

One method of simulation this behavior is to detect the function argument types.

NOTE: As stated below in the comments, the code does not consider null as an argument and will error if null is received. To prevent this error use this if statement to also check for null.

```
if (typeof x === 'object' && x != null)
```

0:05 - What is Function Overloading?
0:24 - Example of Expected Behavior
1:38 - Simulate an Overloaded Function
2:37 - Recommended Alternative to Overloading
3:19 - Say Hello to Dumpling!
Рекомендации по теме
Комментарии
Автор

Your solution will fail if we do doThing(null). Null is also an object in JS, as you klnow. Once in the method, execution will fall into the if statement and we'll end up having an error kinda "Cannot read property 'x' of null"

krnel_errr
Автор

Thanks Joel for the content .... keep going 🙏🏻🤝

kishoreandra
Автор

Thanks for your videos. Curious what you are using to annotate VSCode?

BenNewton
Автор

Only video I've ever seen with 0 dislikes nice

aroncanapa
Автор

hi, what's the extension your using to view the console log right next to the code. Thanks!

transcendingthedyad
Автор

how to "simulate" operator overloading ?

Negative
Автор

Joel can I have a request please! React testing!!

stephenmooney