What is the idiomatic way to subclass Function in ES6?

I am doing this now:

const funtype = Object.create(Function.prototype); funtype.compose = function (foo) { return Object.setPrototypeOf(x => this(foo(x)), funtype); }; const dbl = Object.setPrototypeOf(x => 2 * x, funtype); const inc = Object.setPrototypeOf(x => x + 1, funtype); const sqr = Object.setPrototypeOf(x => x * x, funtype); const notFound = dbl.compose(dbl).compose(inc).compose(sqr); console.log(notFound(10)); 

However, the MDN Object.setPrototypeOf for Object.setPrototypeOf warn us not to use it. Is there a better way to subclass Function in ES6? It is preferable not to use the new class syntax.

+5
source share

Source: https://habr.com/ru/post/1275112/


All Articles