In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The knowledge of this article "javascript how to achieve function overloading" is not quite understood by most people, so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve function overloading in javascript" article.
Javascript can implement function overloading. There is an arguments object inside the javascript function. You can get the parameter list through the arguments object, and determine the number of parameters using different methods to simulate the effect of function overloading.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
Does the function of JavaScript support overloading? Can I overload the function? For this problem, there are two main points: first, the function of JavaScript; second, overloading.
First of all, let's talk about overloading. The so-called overloading, to put it simply, is the case in which the function or method has the same name, but the parameter list is not the same. The functions or methods with different parameters of the same name are called overloaded functions or methods. Therefore, overloading mainly requires two points: first, the same function name. Second, different function parameters.
With the definition of overloading clear, let's go back to JavaScript. Going back to the source, when we talk about JavaScript, we can think of ECMAScript, that is, the standard of JavaScript. So, what specifications are made for functions in this standard?
First of all, ECMAScript has no concept of a function signature because its parameters are represented by an array containing zero or more values. Without a function signature, real overloading is impossible.
Second, if two functions with the same name are defined in ECMAScript, the name belongs only to the later defined function, as follows:
Function add (num) {return num+1;} function add (num) {return num+2;} var result = add (4); / / result is 6
In the above example, the add () function is defined twice, however, when we call it, we call the second function directly, which means that in JavaScript, the later defined function overrides the previously defined function.
At this point, is it possible to determine that JavaScript does not support function overloading?
Let me introduce an arguments object in JavaScript. First of all, the parameters of the ECMAScript function are a little different from those of other languages. The ECMAScript function doesn't mind the number and type of parameters passed in. That is, after you define that the function accepts only two arguments, you can still pass zero or more arguments when calling. This will not be wrong. The reason lies in the arguments object. In ECMAScript, the parameters of a function are always stored in an array that can be accessed through the arguments object. Therefore, you only need to use the length attribute to determine how many parameters are passed when the function is called.
Speaking of which, we can try to write:
Function add (num1, num2) {if (arguments.length = = 1) {alert ("you entered only one number: + arguments [0] +" Please re-enter ");} else if (arguments.length = = 2) {alert (" the sum of the numbers you enter is: "+ arguments [0] + arguments [1]);}}
From this example, we can see that the JavaScript function can react differently by checking the number of parameters passed in the function, which can indirectly achieve the purpose of overloading.
Therefore, JavaScript can mimic the overloading of functions.
The above is about the content of this article on "how to achieve function overloading in javascript". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.