JavaScript function overloading

Source: Internet
Author: User

The JavaScript function does not support overloading. Sometimes we want to define overloaded methods like Java, C and other languages, so we can define overloaded functions in other ways. Of course, there are many ways to achieve it, and everyone may be different. Below I posted my implementation and use of the way to communicate with you.

JS Source:

/** *  function Parameter overloading method  overload, the function parameters are pattern-matched. The default dispatcher support * and ... and?, "*" denotes an arbitrary type of argument, "..." to denote multiple arguments of any  * type, "?" Generally used in ",?..." means 0 or any number of parameters  *  @method  overload *  @static  *  @param  {funcmaps }  List of functions to accept calls based on match  *  @return  {function}  overloaded functions  */var overload = { Map: function (arr, callback, pthis) &NBSP;{VAR&NBSP;LEN&NBSP;=&NBSP;ARR.LENGTH;VAR&NBSP;RLT  = new array (len);for  (var i = 0; i < len; i++)  {if  (I in arr)  rlt[i] = callback.call (Pthis, arr[i], i, arr );} Return rlt;},getobjectclassname: function (obj)  {if  (obj &&  Obj.constructor && obj.constructor.tostring ())  {var arr =  Obj.constructor.toString (). Match (/function\s* (\w+)/);if  (arr && arr.length == &NBSP;2)  {return arr[1];}} Return&nBsp;undefined;},overload: function (dispatcher, func_maps)  {if  (! ( dispatcher instanceof function))  {func_maps = dispatcher;dispatcher =  function (args)  {var ret = [];return overload.map (args, function (o)  {return  overload.getobjectclassname (o)}). Join ();}} Return function ()  {var key = dispatcher ([].slice.apply (arguments));for  (var  i in func_maps)  {var pattern = new regexp ("^"  + i.replace (" * ", " [^,]* "). Replace (" ... ", ". * ")  + " $ ");if  (Pattern.test (key))  {return  Func_maps[i].apply (this, arguments);}}};

Example:

var test = Overload.overload ({"String,string": function (b) {return a + B;}, "Number,number": function (b) {return a + B;} , "String,date": function (A, b) {return a + b.tolocalestring ();}}); Console.log (Test ("Hello", "world!")) Console.log (test) console.log (Test ("Now Time is:", new Date ()))

Result

Hello world!

3

Now time is:2016-07-14 21:07:29


Of course, custom types are also supported:

var test = Overload.overload ({"String,string": function (b) {return a + B;}, "Number,number": function (b) {return a + B;} , "String,date": function (A, b) {return a + b.tolocalestring ();}, "Student": function (s) {return ' name ' is ' +s.name+ ' and ' age ' Is "+s.age;}}); function Student (name,age) {this.name = Namethis.age = Age}console.log (Test (New Student ("Zhang San", 23)))

Result

Name is Zhang San and is 23


Of course you can also use wildcards, here is not an example.


Attention:

When calling an overloaded function argument list with the same type and number of two functions, the latter is called;

When wildcards and non-wildcard functions satisfy both conditions, the former is called in the defined order.

This article from "Tears Dream of the Red" blog, please be sure to keep this source http://wangywei.blog.51cto.com/7093660/1826527

JavaScript function overloading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.