The technique of "recommending" _javascript using the Gerty function to implement Bind method in JavaScript

Source: Internet
Author: User

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, in 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 pretreatment;

The function of the bind method: To process the This in the callback callback method that is passed in as context contexts;

/**
* Bind method Implementation principle 1
* @param callback [function] callback function
* @param context [Object] contexts
* @returns {Function} changes This is the function to point to
/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));
  }
/**
* 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);
  }

Above this javascript use the Gerty function to implement the Bind method "Recommendation" is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.