overloading vs overriding in javascript
Asked Answered
V

4

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.

Vladamar answered 17/12, 2015 at 14:27 Comment(5)
overloading doesn't really exist in JavaScript in the same way as other languages. What specifically were you asked?Maturate
To basically explain the difference. at a company that is transitioning from Java to NodeJSVladamar
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.Acrylonitrile
Aside from the fact the doesn't have overloading, to my knowledge, at all.Acrylonitrile
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.Unity
S
17

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/

Susceptive answered 17/12, 2015 at 14:30 Comment(3)
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.Dryclean
@Dryclean 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 differentKithara
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.Dryclean
S
3

There's a nice example of 'faking' JavaScript function overloading here: https://mcmap.net/q/53451/-function-overloading-in-javascript-best-practices-closed

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.

Seaton answered 17/12, 2015 at 14:34 Comment(0)
D
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.

Dryclean answered 17/12, 2015 at 14:39 Comment(0)
A
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.

Annapolis answered 17/12, 2015 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.