Function Constructor Declaration in javascript examples
- Admin
- Dec 31, 2023
- Javascript
javascript Function Declaration
Javascript is a programming language which is unique features compared to another programming language. Function object construction is one of the cool features.
Functions create using the name and function keyword.
Functions will be helpful to execute the repetitive code and avoid duplicate code writing.
Function features:
1. create using task processing.
2. Returns data to the caller
3. Will take parameters
4. These are objects in javascript.
Functional Declaration Syntax
function nameoffunction(parameters){
//statements
return something or void}
There are many ways we can create a function object using-declaration
Simple functional declaration
It creates using the function keyword and function name.
example is
Function add(a,b){
return a+b;
}
add(3,5); returns 8 as output
Functional expression example
It is another way of creating a function object.
let addFunction=function(a,b){
return a+b
};
addFunction(2,4) returns 6 as output
Array Function expression examples
It also creates functions without a name. It is a shorter syntax and is introduced in the latest javascript language.
let addF=(a,b){
return a+b}
addF(4,5) returns 9 as output
Using Function Constructor Object
It is an alternative way of creating Function object In a javascript, any object can be created using the new operator, For number - new number. In the same way, Function Object is created using the new Function() method boolean, String, Date are some of the primitive types. remaining everything is objects.
new Function({ arguments }, body);
Parameters are arguments that are parameters to the Function object.
The body is a javascript expression enclosed in quotes.
Function Constructor example var add = new Function(); console.log(add) //returns nothing console.log( add(2,7)) //returns undefined
var add1 = new Function("p", "q", "return p + q");
console.log(typeof add); //returns 'function'
console.log(add1(2, 7)); //returns 9
Using Function Constructor and function declaration keyword both provides the same functionality and the difference is in terms of syntax.
Function inherits from Function.prototype which inherits from Object.prototype. Object.prototype is a superclass for all classes So the function can reuse properties and methods available in Function.prototype and Object. Function Constructor and Function Declaration have some differences in terms of the local and global scope of Function. The constructor is to use global context and return the value not from where it is being called. whereas function constructor has the scope of local where it is declared.
var value = 50;
function FunctionDeclaration1() {
var value = 30;
return new Function("return value;"); // value is returned from global variable value
}
function FunctionDeclaration2() {
var value = 60;
function innerfucntion() {
return value; // this returns local variable value
}
return innerfucntion;
}
var function1 = FunctionDeclaration1();
console.log(function1()); // 50
var function2 = FunctionDeclaration2();
console.log(function2()); // 60