Make JavaScript easy to support function overloading (Part 2

Source: Internet
Author: User
Tags static class

In "Make JavaScript easy to support function overload (Part 1-design)," we designed a method that describes the function overload in JavaScript, which relies on a static class called Overload, and now we're going to look at how to implement this static class.

Identifying text signatures

Let's take a look at the overload use cases mentioned in the previous article:

var extend = Overload
  .add("*, ...",
    function(target) { })
  .add("Boolean, *, ...",
    function(deep, target) { });

We allow the user to enter a string representing the signature of an overload. When the user calls the function, we need to take the user input parameter instance to compare with each parameter type on the signature, so we need to convert the string to an array of types first. In other words, the string "Boolean, number, array" should be converted to an array [Boolean, number, array].

Before making the conversion, we first consider dealing with two special types, representing any type of "*" and representing any number of "...". We can define two proprietary types for them to make special compatibility handling within Overload:

Overload.Any = function() {};
Overload.More = function() {};

After these two types, the string "boolean, *, ..." is correctly converted to an array [Boolean, Overload.any, Overload.more]. Since both Overload.any and overload.more are functions, nature can also be regarded as a type.

Once these two types are properly processed, we can begin writing a transformation function that recognizes the text signature:

if (signature.replace(/(^\s+|\s+$)/ig, "") == "") {
  signature = [];
} else {
  signature = signature.split(",");
  for (var i = 0; i < signature.length; i++) {
    var typeExpression = signature[i].replace(/(^\s+|\s+$)/ig, "");
    var type = null;
    if (typeExpression == "*") {
      type = Overload.Any;
    } else if (typeExpression == "...") {
      type = Overload.More;
    } else {
      type = eval("(" + typeExpression + ")");
    }
    signature[i] = type;
  }
}

I think this code is fairly easy to understand, so it's no longer explained. The first time I wrote this code, I forgot to write the first if, so that the blank signature string "" could not be properly identified as an array of blank signatures [], but luckily my unit test code discovered the flaw at the first moment. It seems that writing the unit test code is still very important.

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.