8

In a recent JavaScript interview I was asked about overloading vs overriding. I know this is a concept in Java. But is there something similar in JavaScript, and if so what would be code examples? My understanding is that overloading isn't common in javascript. Why would you need to use "overloading" in JS?

OverRiding is a bit clearer to me - an example of over riding would be in subclassing where you are inheriting from a super class but over riding some methods/properties to create unique ones for a sub class.

Naing Lin Aung
  • 3,373
  • 4
  • 31
  • 48
devdropper87
  • 4,025
  • 11
  • 44
  • 70
  • overloading doesn't really exist in JavaScript in the same way as other languages. What specifically were you asked? – James Thorpe Dec 17 '15 at 14:29
  • To basically explain the difference. at a company that is transitioning from Java to NodeJS – devdropper87 Dec 17 '15 at 14:31
  • 4
    Don't approach the question from a Java developer's classical OOP perspective. Javascript is not OOP in the same way as Java, it is prototypical inheritence. – Rob Foley Dec 17 '15 at 14:31
  • Aside from the fact the doesn't have overloading, to my knowledge, at all. – Rob Foley Dec 17 '15 at 14:32
  • Overloading is not supported in javascript - there's no restriction on the number or order of input parameters therefore only one method with a certain name can exist. Overriding is supported via prototypal inheritance. – Jodi Supporter Dec 17 '15 at 14:34

4 Answers4

16

JavaScript does not support overloading.

JavaScript supports overriding, so if you define two functions with the same name, the last one defined will override the previously defined version and every time a call will be made to the function, the last defined one will get executed.

more read here http://blog.mastykarz.nl/overloading-functions-javascript/

Rajveer gangwar
  • 715
  • 4
  • 14
  • hmm this is not really true. Javascript does not handle it in the "canonical" way but there are many patterns to handle overloading in javascript. As a good javascript programmer it is important to deal with them. – Maksim Dec 17 '15 at 14:41
  • 2
    @Maksim I am certain that JS doesn't support Overloading.. And in JS your function is just another object so a variable is just a reference to that object when you reassign that variable the reference to that variable is lost and the object is GCed.. And the new function sort of replaces but not overrides the actual one.. In overriding reference to both function should exist just the scopes are different – Minato Dec 17 '15 at 15:37
  • 1
    I said that there is no overloading but that there are some patterns to have something similar in functionality. Lets say the options pattern. And of course a function is an object and javascript doesn't "track" functions using signature but just the var name. The overloading is just the way of handling different type of arity or data types that are passed to the function. So yes in javascript there is Overloading but it is done using patterns instead of be native in the language. – Maksim Dec 17 '15 at 16:14
3

There's a nice example of 'faking' JavaScript function overloading here: https://stackoverflow.com/a/457589/2754135

You basically use a parameter in your function that takes an object, that object contains any number of parameters you want.

It's not actually overloading obviously, cause that's not possible in JavaScript.

Community
  • 1
  • 1
3

there is no need for the traditional concept of overload in javascript, because of its dynamic nature. In the more traditional programming languages, as Java, you can define a method multiple times with different signatures and the language will correctly use the method you want to call just using the signature: thats called overload of methods. On the other hand override is the possibility to redefine a method of the parent class in the child class. To do overload in javascript it is common practice to use last parameter that is called options. For example

function yourFunction(firstParam, secondParam, options) {};

the options is just a javascript object that have props you want to pass. Then you can use the so called "options" pattern to check for props. To do override it is more difficult in pure javascript because of the prototypal nature of the language: when you "extend" a base object with a new one, you can use .call() function of the constructor object passing this to decorate the newly created object with the parent props.

Maksim
  • 215
  • 2
  • 12
1

While JavaScript does not support overloading in a traditional sense,

More than the required arguments may be passed at any time to a JavaScript method, and accessed through the arguments variable. This is functionally similar.

Andrue Anderson
  • 664
  • 4
  • 17