Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (7k points)

What are the best ways to fake a function overloading in Javascript?

1 Answer

0 votes
by (13.1k points)

The best way to do function overloading with parameters is not to check the argument length or the types. Checking the types would make your code slow.

What you can do is tack on an object as the last argument to their methods. This object can hold anything.

function fun(a, b, options) {

  // ...

  if (options['test']) { } //if test param exists, do something.. 

}

foo(1, 2, {"method":"mutiply"});

foo(3, 4, {"test":"equals", "bar":"fruit"});

Want to be a full stack developer? Check out the full stack developer course from Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...