I came across two javascript functions, f1 and f2:
const expression = "model.test ==0";
const x = ["model"].concat(`return ${expression};`);
const f1 = Function.apply(Function, x);
const f2 = Function.bind.apply(Function,x)();
I figure out f1({ test: 1 }), it will return false while f1({ test: 0 }) will return true.
I don't know how to call f2 at all. The two keywords Function inside each function definition confuses me completely.
Can someone explain what is happening here?
The original function is ad following
export function evalStringExpression(expression: string, argNames: string[])
{
try {
return Function.bind.apply(Function, [void
0].concat(argNames.concat(`return ${expression};`)))();
} catch (error) {
console.error(error);
}
}
// to call :
hideExpression = evalStringExpression(hideExpression, ['model', 'formState']);
}
f_hideExpression = (model, formState) => hideExpression(model, formState);
I don't know f_hideExpression is value or function or object.
The code is from https://github.com/formly-js/ngx-formly/search?q=evalStringExpression&unscoped_q=evalStringExpression
it creates a function based on expression input. It works pretty magically, I am trying to figurate out how.
Functionas 1st argument forapplysimply setsthis, see developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…