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
Release: 2016-05-16 16:40:27
Original
1157 people have browsed it

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
Copy after login

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
Copy after login

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
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template