Function overloading in JavaScript is well understood

  • 2020-03-30 03:39:22
  • OfStack

There is a special data type in JavaScript, the Function type, and every Function in JavaScript is an instance of the Function type. Because functions are objects, the function name is actually a pointer to a function object and is not bound to a function.


<pre name="code" class="html">function sum(num1,num2) 
{ 
return num1 +num2; 
} 

alert(sum(10,10)); //20 
var other = sum; 
alert(other(10,10)); //20 
sum = null; 
alert(other(10,10)); //20

Using function names as Pointers to functions helps you understand why there is no concept of function overloading in ECMAScript


function sum(num1) 
{ 
return num1 +100; 
} 
function sum(num1) 
{ 
return num1 +200; 
} 
alert(sum(200)); //400

Although two functions of the same name are declared, the following function overrides the previous function, which is equivalent to the following code


function sum(num1) 
{ 
return num1 +100; 
} 
sum = function(num1) 
{ 
return num1 +200; 
} 
alert(sum(200)); //400

Related articles: