I saw this code while working on a project and am not sure what it does exactly. Someone please educates me. Thanks.
var f = new Function('return '+terms)($element);
return f($element);
The new Function()() looks to me like an Immediately-Invoked Function Expression and if it is it would return the value from 'return '+terms' expression, correct? Then why is f being called as a function in the return statement?
Updates:
- terms is an array containing just one element 'required', $element is a jQuery object of an HTML element
- Code execution never gets past the
new Function()
statement with the error message Uncaught ReferenceError: required is not defined. So insidenew Function()()
the function required() is invoked. - I've never seen
new Function()()
syntax before. If it is an IIFE shouldn't it be written as(new Function(){})()
instead?