通过 `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);
};
这个问题与我的
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);
};