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?