I came across some code here where the Function constructor is called in an interesting way:
var jscriptVersion; // ...some code
jscriptVersion = new Function("/*@cc_on return @_jscript_version; @*/")();
At first I thought the extra parentheses were a mistake but after testing a bit in the console I thought maybe it was a shortcut to get a return value:
var a = new Function("return 'abc'");
var b = new Function("");
var c = new Function("")();
var d = new Function("return 'xyz'")();
console.log(a, typeof a); // f anonymous() { return 'abc' } , function
console.log(b, typeof b); // f anonymous() { } , function
console.log(c, typeof c); // undefined , undefined
console.log(d, typeof d); // xyz , string
I'm guessing d
is (almost, aside from the new
keyword) functionally identical to:
var d2 = function() { return 'xyz' }(); console.log(d2); // xyz
But again I've literally never seen trailing parentheses after a function expression that is not an IIFE, and I'm surprised that the code defining d2
doesn't result in a SyntaxError.
I looked at MDN but couldn't find clear information on how this would be used.
And is this valid JS or does implementation of double parentheses vary across browsers?