Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Is there any function overloading in javascript language?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail whether there is any function overloading in javascript language. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Javascript does not have function overloads. Functions with different parameters of the same name are called overloading each other, but function overloading cannot be implemented in javascript, because if there is a function with the same name, the following will overwrite the previous one.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Javascript has no function overload

First of all, describe the definition of overloading: functions with different parameters of the same name are called overloads to each other.

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

However, javascript cannot implement function overloading, because the function with the same name will overwrite the previous one.

The js definition function is identified by the function name, and matches the defined parameters according to the order of the defined parameters and the passed parameters. The redundant ones are rounded out, and the ones that are not enough are treated as undefined.

But js also has its own method to achieve overloading, that is, method overloading is realized inside the function by judging the number of parameters.

For example, if a function receives one parameter, it returns the current parameter, if two returns the sum of two parameters, if it is three, it returns the sum of 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 var I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report