JavaScript function Gerty _javascript skills

Source: Internet
Author: User

What is Corrie

This is a conversion process that transforms a function that accepts multiple parameters into a function that accepts a single argument (the first parameter of the original function), and returns a new function that accepts the remaining arguments and returns the result if the other arguments are necessary.

ke physical and chemical function thought: A JS pre-processing idea; using function execution can form a principle of not destroying scopes, storing what needs to be processed in this not-destroyed scope, and returning a small function, after which we perform small functions, In the small function of the previously stored value of the relevant operations can be processed;

The function of Gerty is mainly to preprocess;

The purpose of the Bind method is to process the this in the passed in callback callback method as context contexts;

The implementation principle of the Bind Method 1 code looks like this:

/**
* Bind method Implementation principle 1
* @param callback [function] callback function
* @param context [Object] contexts
* @returns {Function Change this point to the function
*
/function bind (callback,context) {
var outerarg = Array.prototype.slice.call ( arguments,2);//Represents an argument that takes a parameter in the current scope except Fn,context, and the return
function () {
var innerarg = Array.prototype.slice.call (arguments,0);//represents taking all arguments parameters in the current scope;
callback.apply (Context,outerarg.concat (Innerarg));
}

The following code mimics the principle of the bind implementation on the prototype chain

/**
* Imitate the principle of bind on the prototype chain (the thought of Cauchy function)
* @param context [Object] contexts
* @returns {function} to change this point
Function.prototype.mybind = Function Mybind (context) {
var _this = this;
var outarg = Array.prototype.slice.call (arguments,1);
Compatible with
if (' bind ' in Function.prototype) {return
this.bind.apply (This,[context].concat (Outarg));
}
In the case of incompatibility return
function () {
var inarg = Array.prototype.slice.call (arguments,0);
Inarg.length = = 0?inarg[inarg.length]=window.event:null;
var arg = Outarg.concat (inarg);
_this.apply (Context,arg);
}

function of Gerty (currying)

In computer science, Gerty transforms a function that accepts multiple parameters into a function that accepts a single parameter (the first parameter of the original function), and returns a technique that accepts the remaining parameters and returns a new function of the result.

Gerty is the introduction of some parameters in advance to get a simple function. But the parameters that are passed in are stored in the closure, so there are some peculiar features. Like what:

Cases:

var adder = function (num) {return
function (y) {return
num + y;
}
}
var inc = Adder (1);
var dec = adder ( -1);
Inc, Dec is now two new functions that will pass in the parameter values passed in (+ +) 1
Alert (inc),//100 Alert (Dec ()),
//100
alert (Adder (100) (2 );//102
alert (Adder (2) (100));//102

The above is a small series to introduce the JavaScript function Gerty and the way to achieve the bind method, I hope to help you!

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.