gpt4 book ai didi

JavaScript 组合函数

转载 作者:数据小太阳 更新时间:2023-10-29 05:35:23 25 4
gpt4 key购买 nike

我正在阅读一本书,其中包含以下示例:

var composition1 = function(f, g) {
return function(x) {
return f(g(x));
}
};

然后作者写道:“...组合的简单实现,因为它没有考虑执行上下文...”

所以首选函数是:

var composition2 = function(f, g) {
return function() {
return f.call(this, g.apply(this, arguments));
}
};

后面是一个完整的例子:

var composition2 = function composition2(f, g) {
return function() {
return f.call(this, g.apply(this, arguments));
}
};

var addFour = function addFour(x) {
return x + 4;
};

var timesSeven = function timesSeven(x) {
return x * 7;
};

var addFourtimesSeven2 = composition2(timesSeven, addFour);
var result2 = addFourtimesSeven2(2);
console.log(result2);

有人可以向我解释为什么 composition2 函数是首选函数吗(也许有一个例子)?

编辑:

与此同时,我尝试按照建议使用方法作为参数,但没有成功。结果是 NaN:

var composition1 = function composition1(f, g) {
return function(x) {
return f(g(x));
};
};

var composition2 = function composition2(f, g) {
return function() {
return f.call(this, g.apply(this, arguments));
}
};

var addFour = {
myMethod: function addFour(x) {
return x + this.number;
},
number: 4
};

var timesSeven = {
myMethod: function timesSeven(x) {
return x * this.number;
},
number: 7
};

var addFourtimesSeven1 = composition1(timesSeven.myMethod, addFour.myMethod);
var result1 = addFourtimesSeven1(2);
console.log(result1);

var addFourtimesSeven2 = composition2(timesSeven.myMethod, addFour.myMethod);
var result2 = addFourtimesSeven2(2);
console.log(result2);

最佳答案

这只是回答了 composition2 实际上做了什么:

composition2 当您想将 this 作为函数本身的上下文时使用。以下示例显示使用 data.adata.b 结果为 60:

'use strict';

var multiply = function(value) {
return value * this.a;
}
var add = function(value) {
return value + this.b;
}

var data = {
a: 10,
b: 4,
func: composition2(multiply, add)
};

var result = data.func(2);
// uses 'data' as 'this' inside the 'add' and 'multiply' functions
// (2 + 4) * 10 = 60

但是,它仍然打破了下面的例子(不幸的是):

'use strict';

function Foo() {
this.a = 10;
this.b = 4;
}
Foo.prototype.multiply = function(value) {
return value * this.a;
};
Foo.prototype.add = function(value) {
return value + this.b;
};


var foo = new Foo();

var func = composition2(foo.multiply, foo.add);
var result = func(2); // Uncaught TypeError: Cannot read property 'b' of undefined

因为 composition2 (this) 的上下文是未定义的(并且没有以任何其他方式调用,例如 .apply.callobj.func()),你最终会得到函数中未定义的 this

另一方面,我们可以使用以下代码为其提供另一个上下文:

'use strict';
var foo = new Foo();

var data = {
a: 20,
b: 8,
func: composition2(foo.multiply, foo.add)
}

var result = data.func(2);
// uses 'data' as 'this'
// (2 + 8) * 10 = 200 :)

或者通过显式设置上下文:

'use strict';

var multiply = function(value) {
return value * this.a;
};
var add = function(value) {
return value + this.b;
};


var a = 20;
var b = 8;

var func = composition2(multiply, add);

// All the same
var result1 = this.func(2);
var result2 = func.call(this, 2);
var result3 = func.apply(this, [2]);

关于JavaScript 组合函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36662404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com