I have on one occasion found it useful to assign properties to functions before passing them as arguments to other functions.
That looked like this (sorry about any confusion between anonymous functions and variable-assigned function objects, I think they are not the same thing):
"(could strict mode have something to do with this?)"
var funcOne = function(arg1, arg2) { return arg1 + arg2; };
funcOne.process = true;
var funcTwo = function(arg1, arg2) { return arg1 + arg2; };
funcTwo.process = false;
var procFunc = function(argFunc) {
if (argFunc.process) {
return argFunc(1,2);
}
return "not processed"
}
procFunc(funcOne); // 3
procFunc(funcTwo); // "not processed"
I would like to declare an anonymous function and simultaneously assign properties to it so when it is later called as part of a function array stack, conditional code can depend on the property.
Something like:
"(could strict mode have something to do with this?)"
var funcOne = function(arg1, arg2) { return arg1 + arg2; }.process = true;
var funcTwo = function(arg1, arg2) { return arg1 + arg2; }.process = false;
var procFunc = function(argFunc) {
if (argFunc.process) {
return argFunc(1,2);
}
return "not processed"
}
procFunc(funcOne); // "not processed"
procFunc(funcTwo); // "not processed"
Where the unexpected "not processed" is because confusingly, JavaScript is actually assigning the value 'true' and 'false' to the funcOne and Two variables, and it does not throw an error when the conditional if (argFunc.process) is evaluated (regardless, as I suppose I expect, of "strict mode").
I have gotten this to work functionally anonymously using an IIFE that assigns the 'anonymous' function to a variable then assigns the property on that variable and returns that variable out of the IIFE, but I was hoping there was a JavaScript syntax for this or just a better way, maybe.
To my sensibility, the dot property assignment I tried does not make sense. What if programmer wanted to assign multiple properties to the anonymous function?
I had a short lived hope with var funcOne = function (arg1, arg2) { return arg1 + arg2; } = { process: true }; but that throws a SyntaxError: Invalid left-hand side in assignment.