An example of JavaScript overload function Analysis _javascript Skill

Source: Internet
Author: User

The concept of overloaded functions is not in 1.javascript!

First of all, JavaScript is not the concept of overloaded functions, a long time ago, I used JavaScript to do the Web page, write some simple effect, do not need to use overloaded functions, when writing games, there are a large number of functions, you want to use overloaded functions, I didn't think JavaScript didn't support it.

Let's simply "simulate" the overloaded function in two different ways.

2. According to the number of parameters to judge

JavaScript function has a variable called arguments, is a record parameter of an array, we can use this to determine the number of parameters, and then perform a different content, that is, the same function can have different effects, and C + + and other strong-type language overload function or a large difference. You can write this, the parameters are all commented out, tell the use of the function, this function supports up to 3 parameters, the specific parameters in the function to get. Annotations that support overloaded functions be sure to write more points, it will be clearer, preferably with a call example.

/**
* Return sum of A and B and less than Limitnumber
* @param {number} a
* @param {number} b
* @param {Nu Mber} limitnumber
/function Add (/*a, B, limitnumber*/) {
var a,b,limitnumber;
A = Arguments[0];
b = arguments[1];
if (arguments.length = = 3) {
limitnumber = arguments[2];
if (A + B > Limitnumber) {return
limitnumber;
}
}
return a + B;
}

3. According to the different types of parameters to determine

JavaScript has a keyword called typeof, which can be used to determine the type of a variable.

var temp = "Say";  String
var temp = 1;//number
var temp = undefined;//undefined
var temp = null;//object
var temp = {}; Object
var temp = [];//object
var temp = true;//boolean
var temp = function () {}//function 
function TestFunction (a) {
if (typeof (a) = = "Number") {
//do something
}else if (typeof (a) = = "string") {
//do Something
}
}

The above content is small to introduce the JavaScript overload function of the relevant knowledge, interested friends to study together!

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.