Make JavaScript easily support function overloading (part 1-design)

Source: Internet
Author: User

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 content when writing JavaScript. Code To provide one or more simple call portals for functions with 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: Copy code 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:Copy codeThe 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 of parameters:
any type of parameter
suppose we want to write an each function, and iterate its subscript for array, how do I declare the parameter lists of the two function entries for all the members of other types to iterate over? If C # is used, two function entries are described as follows:
void each (ienumerable iterator) {}< br> void each (Object iterator) {}< br> however, in Javascript, an object is not a base class of all types. (100) the result of instanceof object is false, so we cannot use an object to represent 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: copy Code 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:Copy codeThe Code is as follows: var extend = overload
. Add ("*,...",
Function (target ){})
. Add ("Boolean ,*,...",
Function (deep, target ){});

Summary
In this articleArticleWe 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.

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.