In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to use the number of parameters to achieve overloading function in JavaScript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Examples are as follows:
Function overload () {switch (arguments.length) {case 0: console.log ("have no friends"); break; case 1: console.log ("have one friend"); break; case 2: console.log ("have two friends"); break; case 3: console.log ("have three friends"); break Case 4: console.log ("have four friends"); break; / / etc}}
Overloading can be achieved in this way, but the code is relatively long, and there are sometimes a lot of situations in actual development. So we can use the following method.
_ window.onload=function () {var cat= {friends: ["mimi", "pp", "gg"]} addMethod (cat, "sayName", function (amemb) {console.log ("have two friends");}) addMethod (cat, "sayName", function () {console.log ("none friends") }) addMethod (cat, "sayName", function (a) {console.log ("have a friend");}) addMethod (cat, "sayName", function (a recorder bpenc) {console.log ("have three friends");}) cat.sayName ("xiaoming", "nihao"); cat.sayName (); cat.sayName ("xiaoming"); cat.sayName ("xiaoming", "xiaohong") } / / implement overloading, using the differences of arguments.length to implement function addMethod (object,name,fn) {var old=object [name]; object [name] = function () {if (fn.length==arguments.length) return fn.apply (this,arguments); else if (typeof old=='function') return old.apply (this,arguments);}}
This technique takes advantage of closures, where different parameters are stored as references in closures.
When the addMethod function is actually called, as shown in the following figure
What causes it?
Because of the closure, the variable old outside the object [name] literal function is called in the addMethod function, which makes the garbage collection mechanism will not collect old, so the old will always be in memory and will not disappear. We use this feature to achieve inheritance.
When we execute sayName below, we will look for the corresponding parameters in turn along the references stored above, and then find the corresponding function to execute.
There are still shortcomings in this approach:
1. Overloading only applies to a different number of parameters, but does not distinguish between types, parameters, or other things.
two。 This method has the overhead of function calls because closures take up some memory. Not suitable for high-performance applications.
This is the end of the content of "how to use the number of parameters in JavaScript to achieve overloading function". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.