Reputation: 13497
If you open your console and type Function
, it auto completes it for you, indicating that the indentifier Function
is part of the winddow object. It appears as though the Function
identifier refers to an empty anonymous function.
What is the point of the Function
identifier on window
?
Upvotes: 0
Views: 41
Reputation: 816532
Function
is a global variable. Global variables are properties of the global object. In browsers, the global object is window
.
If you are asking what the purpose of Function
itself is: It's a constructor function (just like Object
, Array
or RegExp
) to create new function objects. It lets you create a new function from code contained in a string (almost like eval
). E.g.:
var myFunc = new Function('return 42;');
console.log(myFunc()); // 42
Functions created this way behave like they had been declared in global scope, i.e. they do not close over the scope they actually have been created in.
This can be useful for browser tools which evaluate user provided JS code provided, like the Babel REPL.
Upvotes: 4