Home > Web Front-end > JS Tutorial > body text

Implementing function overloading and parameter default values ​​in JavaScript

高洛峰
Release: 2016-11-26 14:08:54
Original
1656 people have browsed it

Parameter default value means that if an actual parameter is omitted when calling a function, the function will automatically assign a default value to the parameter, greatly improving the convenience and flexibility of function calling.

For example, take the string interception function substr(string,start,length) in PHP. When length is not specified, the function will intercept the start position in the string to the end of the string by default. If length is specified, then Intercept the string with length starting from the start position, so if you call substr('http://www.hualai.net.cn',11,6), hualai will be returned; if you omit the last One parameter, substr('http://www.hualai.net.cn',11), returns hualai.net.cn.

For example, in the jQuery framework, the $(selector).html() method is to obtain the HTML code within the element, while $(selector).html(content) is to set the HTML within the element. We know that in C language, we can set default values ​​for function parameters in the following form:


void foo(int a, int b = 1, bool c = false);



In Java, you can set the default value of function parameters through function overloading:

public void foo(int a){
foo(a, 1);
}
public void foo(int a, int b){
foo(a, b, false);
}
public void foo(int a, int b, bool c){
//Function content
}




And in JavaScript , how to set the default value of function parameters like jQuery? There is no method in JavaScript to assign values ​​directly after the parameters when defining a function in C language, nor is there any function overloading like in Java. However, we can achieve this through an arguments read-only variable array in the JavaScript method, as follows:

function foo(){
var a = arguments[0] ? arguments[0] : 1;
var b = arguments[1] ? arguments[1] : false;
//Function content
}



The above is by judging whether the parameter exists. If it does not exist, the default value is attached to the variable. We can overload by judging the type of the parameter:

function foo(){
if(typeof arguments[ 0] == 'string')
  alert('The parameter type is a string');
else if(typeof arguments[0] == 'number')
  alert('The parameter type is a numeric value');
}


or

function foo(){
if(arguments[0].constructor == String)
alert('The parameter type is a string');
else if(arguments[0].constructor == Number )
          alert('The parameter type is a numeric value');

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