Learn various properties and usage of Functions as an Object
Functions are essentially object, every function is an instance of the Function type and has properties and methods like object.
And just like objects, function names act as pointers to the function objects and not necessarily attached to the functions.
The demo below shows how function names are just pointer to the function ,like anyother variable pointing to an object. Hence, multiple names can be assigned to a single function
There is just one difference between a Function Declaration and Function Expression, though they are almost equivalent.
Function declarations are read and made available in an execution context before any of the code is executed, while function expressions are not complete until the execution reaches that particular line of the code.
In the below demo, for the first function callingSomeFunction() can get any function as an argument, it just executes it.
Just like passing Functions as a Variables , you can also return a function from a function.
In the demo below, there is function within a function preceded by the operator return. The argument propertyValue is accessible from the inner function, the bracket notation is used to retrieve the value of the property.
When a function is called within the global scope of the webpage, the object this points to the window object.
In the demo below, the function myPlanet() is defined globally but references the this object, the function is assigned to the object aliens and then the call aliens.myPlanet() is made, which points to object aliens and hence this.color returns mars
Since Functions are nothing but object, they too can have properties and methods to describe them.
The properties describing the function are length and prototype, the property length returns no of argument the function has, while prototype specifies the actual location of all instance methods for reference types.
The methods specify some operation that the function has to perform.
In the demo below, both the methods call the function with a specific this value, the method attach() accepts two arguments : this (i.e the current function) and an argument objects.