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

The method of realizing overloading function by using the number of parameters in JavaScript

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

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge points about the method of JavaScript using the number of parameters to achieve overloading function. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

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.

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.

These are all the contents of the article "JavaScript uses the number of parameters to achieve overloading function". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more 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.

Share To

Internet Technology

Wechat

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

12
Report