I’ve been trying to create a function (or many functions) where I can change its inner workings based what’s in a [object] property (which should be possible since a function is also an object).
I did the lesson on Factory Functions that make objects a while ago.
And I tried to make a similar factory-function-type thing to make functions.
(The only related thing I’ve found so far is that in JavaScript, you can’t make objects callable … for the most part.)

This is what I have so far … after some trial and error.

function quadraticMaker(a, b, c) {
let quadratic = function(x) {
return (quadratic.a * x * x) + (quadratic.b * x) + quadratic.c;
};
quadratic.a = a;
quadratic.b = b;
quadratic.c = c;
let linear = function(x) {
return (linear.m * x) + linear.b;
}
linear.m = b;
linear.b = c;
let constant = function(x) {
return constant.value;
}
constant.value = c;
if (a == 0 && b == 0) {
return constant;
}
else if (a == 0) {
return linear;
}
else {
return quadratic;
}
}
let f = quadraticMaker(1, 0, -4);
console.log(f);
console.log(`a=${f.a}, b=${f.b}, c=${f.c}`);
console.log("f(0) = " + f(0));
f.a = 1;
f.b = 3;
f.c = 2;
console.log(f);
console.log(`a=${f.a}, b=${f.b}, c=${f.c}`);
console.log("f(0) = " + f(0));
console.log("f(1) = " + f(1));

The code seems to work just fine, but …

Does anyone know a better way to do this?
Is there something I should know to be able to accomplish this?

Not sure I really like that example without some context. One of my usual go-to factory examples is the log function. It produces tangible outcomes and doesn’t need a curve to help it make sense.

const log = base => x => Math.log(x) / Math.log(base)
const log2 = log(2)
console.log(log2(64)) // 6

Where factory functions shine the most is in their transience. We can produce them out of thin air, use them and discard immediately. It leans toward functional and declarative programming which both tend to help in a dynamic environment.

console.log(log(2)(64)) // 6

Background

The built in, Math.log() method uses base, e. Granted there may be other built-ins for base 10 or base 2, but we don’t need them. We have the exponent laws that tell us we can convert logs of different bases. Given that, what more do we need? The code becomes self explanatory since it is merely an expression of that law.

Thanks for the example.
A factory function in one line … wow log(stuff) returns a (logarithmic) function [that, in turn, uses stuff as the base of the log].
Excellent!

There is a tonne of meaning in that. It means that through code we can express all the laws of physics and mathematics and make it behave accordingly. Of course this takes some hands on to deal with the contingencies, but as above, it is possible.

Say you have an exploration into kinematics. One of the variables is force of friction. In air we have one coefficient that could be given to a factory function so we can compute a range of objects in motion through air at the same time, using a simpler function.

So we can deduce that factory functions lessen the degree of complexity. This is the ponderable, and therefore worthy of examination, conclusion.