通过' bind '将函数参数传递给另一个函数
Pass function arguments into another function via `bind`
这个问题与我之前的问题有关,所以我将在这里添加与示例几乎相同的代码。
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
值,并且可以访问第二个函数的this
和mX1, mY1, mX2, mY2
参数。
有可能吗?我该怎么做呢?
可以这样使用arguments
对象吗??
_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);
};
相关文章:
- 如何在JQuery函数中定义一个值,然后调用另一个函数并使用该值
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 在另一个函数中使用变量this
- 在另一个函数成功结束后调用该函数
- 多次调用另一个javascript函数中的javascript函数
- 使用JS函数来使用另一个函数的语法?node.js
- 嵌套到另一个函数中的Fancybox函数;不起作用
- 如何从onclick函数设置全局变量并将其传递给另一个JS文件
- javascript函数将数据添加到屏幕,但随后被另一个函数覆盖
- 另一个Ajax函数触发的Ajax函数不起作用
- 如何从另一个处理程序内部取消JavaScript事件处理程序函数的执行
- 如何在不预定义的情况下将javascript函数传递到另一个函数中
- 从另一个函数获得$this值
- 如何记录调用另一个函数的函数的返回值
- 它在另一个函数中嵌套后不会输出文本
- 如何将自动完成的值传递到另一个函数中
- 将一个函数作为参数传递给javascript中的另一个函数
- 同一事物的两个函数,一个崩溃,另一个不崩溃,为什么
- 从另一个函数延迟解析的返回
- 多次调用promise函数,直到另一个promise函数满足条件