The anti-curry of JavaScript (uncurrying)

Source: Internet
Author: User

in JavaScript, when we call a method of an object, we don't really care whether the object was originally designed to have this method, which is the characteristic of the dynamic type language. Can be implemented by the anti-curry (uncurrying) function, allowing an object to borrow a method that would otherwise not belong to him.

Usually let the object to borrow a method that originally does not belong to it, can be implemented with call and apply, as follows

One of the more common scenarios is to allow class array objects to borrow array.prototype methods;

(function() {    Array.prototype.push.call (arguments,4)    console.log (arguments); // [1, 2, 3, 4]}) (a)

Extension: Why are class array objects able to borrow array methods? may wish to understand under V8 's engine source code, takes Array.prototype.push as an example:

functionArraypush () {check_object_coercible ( This, "Array.prototype.push"); varArray = To_object ( This); varn =To_length_or_uint32 (Array.Length); varm =%_argumentslength (); .......   for(vari = 0; I < m; i++) {Array[i+n] =%_arguments (i); }  varNew_length = n +m; Array.Length=new_length; returnnew_length;}

By this code, it can be seen that Array.prototype.push is actually a process of copying properties, adding parameters to the push object according to the subscript, and modifying the length property of the object, whether it is an array or an array of classes is not important. From the source can be seen, as long as the object itself can access the property, and the length property can read and write, you can borrow the array prototype of the push method.

In this way, where this is used in the method, it is not confined to the original object, but is generalized and has wider applicability. So is there a way to extract the generalization of this process? Then the anti-Curry (uncurrying) is to solve this problem. The topic of uncurrying is from an article published in 2011 by the father of JavaScript, Brendan Eich, and the following code is one of the implementation methods:

function () {  varthis;   return function () {    var obj = Array.prototype.shift.call (arguments);     return self.apply (obj, arguments);  };};

You can then define a push function that is more concise and straightforward to implement a push method that is not limited to an array. As follows:

var push = Array.prototype.push.uncurrying ();(function() {    push (arguments, 4);    Console.log (arguments); // [1,2,3,4]}) (a)

In addition to the first anti-curry implementation, there is another way to achieve this:

function () {  varthis;   return function () {    return  Function.prototype.call.apply (self,arguments)  };}
Reference Books: JavaScript Design patterns and development practices

The anti-curry of JavaScript (uncurrying)

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.