JavaScript: Function Object
Function
is the value of the property key "Function"
of the Global Object
.
console.log( window.Function === Function ); // true
Type
Type of Function
is a function.
console.log( typeof Function === "function" ); // true
Parent
Parent of Function
is Function.prototype
.
[see Prototype and Inheritance]
console.log( Reflect.getPrototypeOf ( Function ) === Function.prototype ); // true
Purpose
Purpose of Function
is:
- To define a function from strings. (Useful when you need to generate function definition at run time).
- Holds the property key
"prototype"
. The value ofFunction.prototype
is the parent object of all function objects.
Function Constructor
Properties
- prototype
length
β every function has a own string property"length"
, including Arrow Function,Function
,Function.prototype
. Its value is number of parameters, not counting Argument Default Value and Rest Parameters .
// every function has a own property βlengthβ console.log( ((x) => x+1).hasOwnProperty ("length") ); console.log( Function.hasOwnProperty ("length") ); console.log( Function.prototype.hasOwnProperty ("length") ); // Its value is number of parameters in the argument, not counting Argument Default Value and Rest Parameters console.log( (() => []).length === 0); console.log( ((x) => [x]).length === 1); console.log( ((x,y) => [x,y]).length === 2); console.log( ((x,y=4) => [x,y]).length === 1); console.log( ((x,...y) => [x,y]).length === 1); // all true