Home > Web Front-end > Front-end Q&A > Does javascript have function overloading?

Does javascript have function overloading?

青灯夜游
Release: 2022-02-23 14:58:07
Original
2513 people have browsed it

Javascript does not have function overloading. Functions with the same name and different parameters are called overloading. Function overloading cannot be implemented in JavaScript because if there are functions with the same name, the later ones will overwrite the previous ones.

Does javascript have function overloading?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript does not have function overloading

First describe the definition of overloading: functions with the same name and different parameters are called overloading.

That is, there are functions with the same name in the program, and different calls of the same function can be realized through parameter judgment.

And JavaScript cannot implement function overloading, because for functions with the same name, the later ones will overwrite the previous ones.

JS-defined functions are identified by function names, and are matched with the parameters passed in according to the defined parameter order. Any excess will be discarded, and any insufficient will be treated as undefined.

But js also has its own method to implement overloading, which is method overloading. By judging the number of parameters, method overloading is implemented inside the function.

For example, when the function receives one parameter, it returns the current parameter. If there are two, it returns the sum of the two parameters. If there are three, it returns the sum of the three functions. And so on...

function add(){
	if(arguments.length === 1){
		return arguments[0]
	} else {
		var _index = arguments.length;
		var allnum = 0;
		for (var i = 0;i<_index;i++){
			allnum = allnum + arguments[i];
		}
		return allnum
 	}
	return arguments.length
}
add(1,1,2,3,3,3,4);
Copy after login

Does javascript have function overloading?

[Related recommendations: javascript learning tutorial]

The above is the detailed content of Does javascript have function overloading?. For more information, please follow other related articles on the PHP Chinese website!

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