Search within TutorialsPark

Javascript Function: Object

Learn various properties and usage of Functions as an Object

Javascript Functions as 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

Example: Javascript Functions: as an Object.

Give it a TRY! » Note: The name of the function is used to access the function pointer, instead of executing it.

JavaScript Function: Function Declaration v/s Function Expression

There is just one difference between a Function Declaration and Function Expression, though they are almost equivalent.

The difference is primarily due to the way the date is loaded into an execution context by a Particular JavaScript Engine.

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.

Example: JavaScript Function : Function Declaration v/s Function Expression

Give it a TRY! » Note: The second code block does not work because, the function itself is a part of the initialization and not declaration.

Javascript Function : Using Function as Values

In JavaScript, Function names are equivalent to variables and hence functions can be used at some place while some other value can be used at someother places.

In the below demo, for the first function callingSomeFunction() can get any function as an argument, it just executes it.

Example: JavaScript Function : Using Function as a Value

Give it a TRY! » Note: The variables multiply2 and message are passed into the callingSomeFunction() instead of their outputs.

Javascript Function : Function Returning Functions

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.

Example: JavaScript Functions : Returning Functions from Function

Give it a TRY! » Note: The compareItFunction sorts based on property provided as an argument.

Javascript Functions : this Keyword

The JavaScript special keyword this is used to create a reference to the context object on which the function is currently operating, also referred as this value.

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

Example: JavaScript Function : this Keyword

Give it a TRY! » Note: The value is this is determined when the function is called, hence it may not be the same throughout the execution.

Javascript Function : Properties

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.

Example: JavaScript Function : Properties

Give it a TRY! » Note: The last function has no argument, hence the length property returns 0.

Javascript Function : Methods

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.

Example: JavaScript Function : Methods

Give it a TRY! » Note: The method product1() executes the method product() with this creating a reference to window object.