JS Implementation overload

Source: Internet
Author: User
Tags switch case

In JS, the common way we implement overloading is:

1. Perform different actions depending on the type of parameter passed in.

2, using the parameters of the special parameter values for different operations.

3, according to the number of parameters to overload.

The implementation of the third overloaded method is described here.

To implement the third method, the simplest is to use the switch case to determine the number of arguments, and then perform the corresponding operation, but this method of judgment led to the code is not very neat, forcing lattice is not too high.

Such as:

function fn () {Switch(arguments.length) { Case 0:                    //EXECUTE statement block                     Break;  Case 1:                    //EXECUTE statement block                     Break;  Case 2:                    //EXECUTE statement block                     Break; default:                     Break; }        }

Here is a method, regardless of whether it is applicable, but you can understand this overload way, let us learn some of the techniques in JS and the closure of some understanding. After all, it is not a bad thing to know more.

First on the code:

 vararr ={value: ["a","b","C"]        }; Bindmethod (arr,"Find", function () {Console.log (0)        }); Bindmethod (arr,"Find", function (a) {Console.log (1)        }); Bindmethod (arr,"Find", function (A, b) {Console.log (2)        }); function Bindmethod (obj, name, fn) {varOld = Obj[name];//there is a separate old object in each method, resulting in a closureObj[name] =function () {if(Fn.length = = arguments.length) {//The fn.length here is the number of method-defined parameters, and the number of Arguments.length passed in is two different things.                    returnFn.apply ( This, arguments);//returns the passed in anonymous function that executes the                }                Else {                    returnOld.apply ( This, arguments);//returns an array of methods, looking up again}}} arr.find (); //0Arr.find (1);//1Arr.find (1,2);//2Arr.find (1,2,3);//uncaught typeerror:cannot Read property ' Apply ' of undefined       /**
The first time binding, the first assignment of old, because Attr.find is not created, so old=undefined. Then create an anonymous method for the Arr object Find property, this anonymous method fn.length=1. The second time you bind, point the old object to the anonymous method you created for the first time, and then point the Arr object Find property to the currently created anonymous method, because old points to the previous method's reference, which causes the previous method to not be freed. The third is also the same step, ensuring that the second bound method is not released. The execution of the overloaded method, like a chain, starts from inside the last bound method to find out if the method defines the number of parameters and whether the number of passes matches, and if the match returns the current incoming anonymous function, continue looking up until it is found. If the result is not found at the end, the current program will error because the first time the method is bound, the old object is a undefined**/

Binding phase

The ARR is bound with three methods, each with an old object, a previously saved method, and so on (in the current method, the old object points to the last bound method, but note that in the first bound method, the old object is Undefind), so that a chain can be formed. That is, there are three attr.find memory spaces in memory, each pointing to an anonymous method in which the Bindmethod is created.

Call Stage
When the 0 arguments are passed, the last bound method is called first. Then, if the incoming parameter is 2, an anonymous method is returned, otherwise
Call the previous method pointed to by old. Enter again, judge again until you find the method that matches the number of arguments, and return to its anonymous function.

In this case, the binding of the Attr object is done with three binding overrides, then the Attr.find is assigned, and finally only the last assignment object is retained.

This is why, at the time of the invocation, the execution starts from the last bound method. The reason for the Attr.find method being overwritten, but not automatically released, is because the new method's old

Always points to the overridden method, so a closure is generated here.

JS Implementation overload

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.