The bind principle comes with some of your own understanding (quoted in JavaScript design pattern and with view practice)

Source: Internet
Author: User

Function.prototype.bind = function () {//this points to all functions generated by the function constructorvar self = This,//save original function//[] is equivalent to Array.prototype (use the Shift method on the array constructor prototype to take the first parameter passed as the object that this point points to)context = [].shift.call (arguments),//The This context that needs to be boundthe//slice function is to return the selected array element ( here [].slice.call (arguments) is the parameter passed within the BIND function ) args = [].slice.call (arguments); Remaining parameters to the array return function () {//return a new function//Here [].slice.call (arguments) is the parameter that needs to inherit the new function of the bind function Return self.apply (context, [].concat.call (args, [].slice.call (arguments)));when executing a new function, the previously passed context is treated as this in the new function body, and the concat function is used to connect two separate arguments .//And combine two parameters passed in separately as parameters of the new function}};var obj = {name: ' Sven '};var func = function (A, B, C, D) {alert (this.name);//output: Svenalert ([A, B, C, d])//input Out: [1, 2, 3, 4]}.bind (obj, 1, 2); func (3, 4);

The bind principle comes with some of your own understanding (quoted in JavaScript design pattern and with view practice)

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.