传递函数参数到另一个功能通过`bind` [英] Pass function arguments into another function via `bind`

查看:209
本文介绍了传递函数参数到另一个功能通过`bind`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是关系到我的<一个href=\"http://stackoverflow.com/questions/31569200/javascript-get-the-parents-prototype-this/31569864\">$p$pvious问题,所以我会添加基本相同code作为例子在这里。

This question is related to my previous question, so I'll add almost same code as example here.

Ease.bezier = function(mX1, mY1, mX2, mY2) {
    return _bezier.processBezier(mX1, mY1, mX2, mY2);
};

var _bezier = Ease.bezier.prototype;

_bezier.processBezier = function (mX1, mY1, mX2, mY2) {
   return _bezier.render; // this is where I need the `this`, mX1, mY1, mX2, mY2 to be passed into the next function
};

_bezier.render = function(aX){ //the aX value here comes from another object
    var mX1 = [bound function attributes[1]]; // I think you can understand what I mean here
    if (mX1 === mY1 && mX2 === mY2) return aX;

    if (aX === 0) return 0;
    if (aX === 1) return 1; 
    return _bezier.computeBezier(_bezier.gx(aX), mY1, mY2);     
};

现在我需要知道,如果我能以某种方式结合的两个功能,而不影响从另一个对象来,并且可以访问到这个 AX 价值 MX1,MY1,MX2,MY2 参数到第二功能。

Now I need to know if I can bind the two functions somehow without affecting the aX value coming from another object and having access to the this, and mX1, mY1, mX2, mY2 arguments into the second function.

这可能吗?我该怎么办呢?

Is it possible? How can I do it?

推荐答案

愿你可以使用的 参数 反对这样的??

May you can use the arguments object like this ??

_bezier.processBezier = function (mX1, mY1, mX2, mY2) {
  return _bezier.render.bind(this, arguments); // <--- bind all the arguments and the context "this"
};

_bezier.render = function(){ // <--- aX is not required anymore instead use arguments object
  var args = arguments[0]; // <--- this corresponds to [mX1, mY1, mX2, mY2]
  var aX = arguments[1]; // <--- this corresponds to aX now
  if (args[0] === args[1] && args[2] === args[3]) return aX; // <--- notice args object here

  if (aX === 0) return 0;
  if (aX === 1) return 1; 
  return _bezier.computeBezier(_bezier.gx(aX), mY1, mY2);     
};

这篇关于传递函数参数到另一个功能通过`bind`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆