function overloading in JavaScript deep understanding of _javascript techniques

Source: Internet
Author: User

There is a special data type---function type in JavaScript, and every function of JavaScript is an instance of a function type. Because a function is an object, 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)); 
var other = sum; 
Alert (Other (10,10)); 
sum = null; 
Alert (Other (10,10)); 20

The function name as a pointer to a function helps to understand why there is no concept of a function overload 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 following function overrides the previous function, which is equivalent to the code below

function sum (NUM1) 
{return 
num1 +100; 
} 
sum = function (NUM1) 
{return 
num1 +200; 
} 
Alert (sum (200)); 400

When you create the second function, you actually overwrite the first function variable of the reference. Sum

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.