Home >Web Front-end >JS Tutorial >In-depth understanding of function overloading in JavaScript_javascript skills

In-depth understanding of function overloading in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:40:271153browse

There is a special data type in JavaScript---Function type. Each function in JavaScript is an instance of Function type. Since functions are objects, the function name is actually a pointer to the function object and will not be 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 the function name as a pointer to the function helps to 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 with the same name are declared, the latter function overwrites the previous function. The above is equivalent to the following code

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

When creating the second function, the referenced first function variable sum is actually overwritten

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn