"Reprint" JS in the Bind method and function currying

Source: Internet
Author: User

Native Bind method

Unlike the Bind method in jquery is just a simple binding event function, native JS in the bind () method is slightly more complex, the method is introduced in ES5, presumably is ie9+ and other modern browsers are supported (about ES5 features of the support case stamp here ECMAScript 5 Compatibility table), the authoritative guide mentions the implementation of the method using apply in ES3 (the chapter of the function in the JS authoritative guide), but it is not true to restore the method, which is also an interesting feature of the true bind method.

The meaning of this passage, if combined with the meaning of the Rhino book context and the Bind () method in Rhino book, is to say that all parameters after the first parameter of the bind () method are used as the starting arguments of the return function. First bind differs from call and apply simply by setting the value of this, and it also binds all the arguments in the incoming bind () method (the parameters after the first argument) with this, and my personal understanding is that the subsequent arguments will also be part of the binding context, as follows: JS Authoritative Guide of the original statement:

The ECMAScript 5 bind () method does more than just bind a function to an object. It also performs partial application:any arguments you pass to bind () after the first is bound along with the This value .

For this feature, see the original chestnut of the authoritative guide:

var return x +var succ = Sum.bind (null1// Let this point to null, Subsequent arguments are also passed into the bound function as arguments sumsucc (2//  = = 3:  You can see that 1 is bound to the X in the SUM function

Second, the length (parameter amount) of the function returned by the bind () method is equal to the number of parameters of the original function minus the number of arguments in the incoming bind () method (that is, the remaining arguments are not passed) because the arguments in the incoming bind are bound to the formal parameters of the original function, Around the dead, with a chestnut:

function  //func has a length of 4var after = Func.bind (null,1,2)  //// Here Two arguments are entered (to be bound to the Func function, a-b//afterlength of 2

Thirdly, when the function returned by bind () is used as a constructor, the this of the incoming bind () is ignored , and the actual participants are all passed in to the original function, which is very abstract, for example, a chestnut?

function original (x) { This. a=1;  This. b =function () {return  This. A +x}}varobj={A:Ten}varNEWOBJ =New(Original.bind (obj,2))//An argument was passed in 2Console.log (NEWOBJ.A)//Output 1, indicating that the returned function is used as a constructor when obj (the value of this) is ignoredConsole.log (NEWOBJ.B ())//Output 3, indicating that the passed argument 2 passed in the original function original

The concept of the function Gerty dialect

The above is the characteristics of the bind method in ES5, this technology is also known as the function of curry, in the reading of the "JS authoritative guide" When you feel this thing forcing lattice too high direct neglect, and recently encountered this concept, simply plan to take him.
The function currying is interpreted in the encyclopedia as:

In computer science, currying is a technique that transforms a function that accepts multiple parameters into a function that takes a single parameter (the first parameter of the original function) and returns a new function that takes the remaining parameters and returns the result. This technique was named by Christopher Strachey, a logic home Curry, although it was invented by Moses Schnfinkel and Gottlob Frege.

The above translates into adult words: this technique turns functions of multiple parameters into functions with only one parameter. The Bind method is the practice of this technique in JS. As a rookie, currently only used bind set this value, code this blog's purpose is to clarify the characteristics of bind.

Original link: 1190000003963461

"Reprint" JS in the Bind method and function currying

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.