Making JavaScript easily support function overloading (Part1-design) _ javascript skills

Source: Internet
Author: User
Does JavaScript support function overloading? Not supported, or supported. This statement is not supported because JavaScript cannot directly write multiple functions with the same name as other languages that support function overloading in the native mode, so that the compiler can determine which overload a call corresponds. Does JavaScript support overloading?
Does JavaScript support function overloading? Not supported, or supported. This statement is not supported because JavaScript cannot directly write multiple functions with the same name as other languages that support function overloading in the native mode, so that the compiler can determine which overload a call corresponds. It is supported because JavaScript functions do not impose any restrictions on the parameter list and support for function overloading can be simulated within the function.
In fact, among many famous open-source libraries, we can see the design supported by the function's internal simulation overload. For example, jQuery's jQuery. extend method determines whether an optional parameter exists based on the parameter type. If it does not exist, shift the parameter to ensure that the subsequent logic runs correctly. I believe many people have written similar code when writing JavaScript, in order to provide one or more simple call portals for function-rich functions.
However, a fundamental problem is that it violates the DRY principle. Each function that supports heavy loads has an extra piece of code used to handle heavy loads based on the number and type of parameters. The code implies repeated logic, but each section is different. In addition, it is not easy to maintain the code, because when you read the code, you cannot see at a glance what are the methods supported by the function, and it is also difficult to maintain the heavy load.
Describe the DSL of the reload entry
I want to describe the reload entry in JavaScript in a simple way. It is best to use function signatures to distinguish between overload entries, just as in other languages, because function signatures are the best DSL in this regard. In my hypothesis, the most JavaScript-compliant overload entry description DSL should be like this:

The Code is as follows:


Var sum = new Overload ();
Sum. add ("Number, Number ",
Function (x, y) {return x + y ;});
Sum. add ("Number, Number, Number ",
Function (x, y, z) {return x + y + z ;});


After describing the overload entry and corresponding function body, the call to the sum function should be like this:
Sum (1, 2 );
Sum (1, 2, 3 );
The above code is clear to me and easy to maintain-you can see the signature of the reload entry at a glance, and it is easy to modify or add a reload entry. But we encountered a problem, that is, the functions in JavaScript cannot be new, and the objects obtained through new Overload () cannot be called, for this reason, we can only make the Overload into a static class, and the static method returns the Function instance:

The Code is as follows:


Var sum = Overload
. Add ("Number, Number ",
Function (x, y) {return x + y ;})
. Add ("Number, Number, Number ",
Function (x, y, z) {return x + y + z ;});


Required reload entry support
Imagine which common JavaScript function entries cannot be described using the above DSL? I know two types:
Any type of Parameter
Suppose we want to write an each function, iterate its subscript for Array, and iterate all its members for other types. How can we declare the parameter list of the two function entries? If C # is used, two function entries are described as follows:
Void Each (IEnumerable iterator ){}
Void Each (object iterator ){}
However, in JavaScript, Object is not a base class of all types. (100) the result of instanceof Object is false, so we cannot use Object to refer to any type, A new symbol must be introduced to indicate any type. Considering that this symbol should not conflict with any possible class names, I chose to use "*" to represent any type. The JavaScript corresponding to the above C # code should be like this:

The Code is as follows:


Var each = Overload
. Add ("Array ",
Function (array ){})
. Add ("*",
Function (object ){});


Any number of parameters
In JavaScript Functions, it is common to require support for any number of parameters. I believe the usage is much higher than the params keyword in C. This cannot be described in our previous rules. Therefore, we need to introduce a symbol that does not conflict with the class name to represent params in C. I chose to use "..." to represent params, which means that multiple parameters are acceptable. Let's take a look at how jQuery. extend's overload should be described:

The Code is as follows:


Var extend = Overload
. Add ("*,...",
Function (target ){})
. Add ("Boolean ,*,...",
Function (deep, target ){});


Summary
In this article, we try to design a function overload method that is applicable to JavaScript and easy to read and maintain. In the next article, we will try to write the Overload class to implement this design.
Related Article

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.