16 March 2009
1 Function Overloading or Method Overloading in JavaScript
Function Overloading or Method Overloading in JavaScript
Though JavaScript is an object oriented language, it is not at all supporting function overloading or method overloading. If you had written more than one function with the same function name, the last one will override all the former. That is, you cannot implement function overloading or method overloading in JavaScript. There is an interesting factor regarding this. Let us consider a situation
You had defined a function with one input parameter, but in the function call you are not passing anything, just calling that function. Even now it will not throw any JavaScript error message; it will simply execute that function.
No matter whether your function definition is with or without input parameter, the function will be simply executed without throwing any JavaScript error. Number of input parameter is immaterial in case of the JavaScript function calls, only function name or the method name matters.
Let’s take a look, what will happen if you try for function overloading in JavaScript.
You are defining a function, say funKnight with no input parameter, and then you are defining the same function with different numbers of input parameters and keeping different alert messages for each definition. Then just try to call this function from anywhere of your page, you can see that irrespective of the number of input parameters (zero or more than zero) you passed while calling the function, the last defined one will be executed. And you will be getting the last definition’s alert message. You can try with the code which I had given below. <html> <head> <script language="JavaScript"> function funKnight(first) { alert("FunKnightfirst"); } function funKnight(first,second) { alert("FunKnightfirst,Second"); } function funKnight(first2) { alert("FunKnightfirst2"); } function funKnight() { alert("Simple-FunKnight"); } </script> </head> <body> <input type="Button" name="inputButton" value="Click here" onClick="funKnight()"> </body> </html>
I had tried this code (method overloading or function overloading) with all the leading browsers such as Internet explorer 7.0 and lower versions, Mozilla Firefox 3.0.7 and lower versions, Google chrome and Opera; I could not implement function overloading anywhere. The last definition did simply override every other. Let’s conclude, till the date Browsers are not supporting function overloading (Method overloading) for JavaScript
1 comments:
You are right, you can’t overload the methods or functions of JavaScript, because JavaScript is loosely typed, so you can't distinguish function signatures based on datatypes of the arguments.
In JavaScript as for differing amounts of arguments or datatypes of arguments, JavaScript really doesn't care about that either.
Function fnKnight(a, b, c) is pretty much the same as function fnKnight() except that in the former, a = arguments[0], b = arguments[1], c = arguments[2] in the namespace of the function.
Please give your valuable comments.