bdadoa
Last Updated: September 27, 2018
·
12.5K
· steveniseki

JavaScript function overloading

This is a simple example of function overloading in JavaScript using closures to overload a Users.find() method, we can overload the method with either a name or a first name, last name.

Thanks to Steve Jansen http://steve-jansen.github.io/ for this example

(function(){
 function findAll() {
   // Find all users...
  }

 function findByFullName(name) {
  // Find a user by name
  }

  function findBySurname(first, last) {
     // Find a user by first and last name
  }

  Users.prototype.find = function find() {
    if (arguments.length === 0)
       return findAll.apply(this);

    if (arguments.length === 1 and typeof(arguments[1]) === 'string')
      return findByFullName.apply(this, arguments);

   // by default, search using first and last name
   return findBySurname.apply(this, arguments);
  }
})();

3 Responses
Add your response

20746
over 1 year ago ·
30040

in find() correctly should be:
if (arguments.length === 1 && typeof(arguments[0]) === 'string')

4 months ago ·
30803

Great article, Here with the help of our article https://windowsclassroom.com/windows-10-recovery-partition you will get the tutorial to learn the process to create recovery partition in windows 10 operating system in easiest way.

20 days ago ·