[Effective JavaScript note] 26th: Using the Bind method to implement the currying of a function

Source: Internet
Author: User

In addition to binding functions to objects, we look at some of the other applications of the Bind method.

Simple example

Example: Suppose you have a simple function that assembles a URL string. The code is as follows

function Simpleurl (protocol,domain,path) {   return protocol+ '://' +domain+ '/' +path;}

To build the path string for a specific site as an absolute path URL. You can use the map method of an array in ES5. As follows

var paths=[' wengxuesong/', ' wengxuesong/p/5560484.html ', ' wengxuesong/p/5555714.html '];var urls=paths.map (function (path) {   return Simpleurl (' http ', ' www.cnblogs.com ', Path);}); urls;//["http://www.cnblogs.com/wengxuesong/", "http://www.cnblogs.com/wengxuesong/p/5560484.html", "http// Www.cnblogs.com/wengxuesong/p/5555714.html "]

The implementation of this function is done correctly.
But is there any place to improve it? Each iteration in the inside uses the same protocol string and the site domain name string. The first two parameters passed to the Simpleurl function are fixed for the iteration, and only the third parameter is changed. You can use the Bind method to generate an anonymous function that already contains two parameters. The code is as follows:

var urls=paths.map (Simpleurl.bind (null, ' http ', ' www.cnblogs.com '));

The call to Simpleurl.bind produces a new function that delegates to Simpleurl. The first parameter of the Bind method provides the value of the recipient. You can use null or undefined because you don't need it here. The remaining parameters of the Simpleurl.bind together with all the parameters provided to the new function form the arguments passed to the Simpleurl.
The above code can be split more finely, the code is as follows:

var newsimpleurl=simpleurl.bind (null, ' http ', ' www.cnblogs.com '); var urls=paths.map (Newsimpleurl);

It is also possible to implement functions without using the Bind method, the following version is implemented using a simple function, the code is as follows

var nobindsimpleurl=function (path) {    return Simpleurl (' http ', ' www.cnblogs.com ', path);} var urls=paths.map (Nobindsimpleurl);

In contrast, the Bind method provides a simpler implementation and simpler structure, but it can be a bit difficult for people who don't understand the bind method.

function currying

The technique of binding a function to a subset of its parameters is called function curry (curring), which is named after the logic family, Haskell Curry.
In comparison to explicit closed functions, the function of curry is a simple technique that uses fewer references to implement a function delegate.

Personal understanding

The function is to curry, just the parameter processing, that is, simplifying the parameters. The schematic is as follows

Tips
    • Use the Bind method to implement the function curry, that is, to create a fixed demand parameter subset of the delegate function

    • Pass null or undefined as the receiver's parameter to implement the function's curry, ignoring its receiver

[Effective JavaScript note] 26th: Using the Bind method to implement the currying of a function

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.