Function Invocation

Programs are assigning memory and executing functions. For example, assigning a value to a variable and then running a function for the program to do something with those variables. Without functions, our programs would not do anything.

  • function declaration: function declaration is something that starts with function keyword.
  • function expression: function expression is something that starts declaring a variable with function value.
    • function expression is not hoisted.
// function expression
var sing = function(){
 console.log("It is defined");
}
  • function invocation: calling a function we do a function invocation by simply running the function with curly brackets.

when a function is invoked, we create a new execution context on top of our global execution context.

Function Invocation

Ways of invoking a function

  • general
  • by call() or apply()
  • Inside object
  • using a function constructor
function one() {
    return console.log(1);
}
const obj2 = {
    two: function () {
        return console.log(2);
    }
}
const obj3 = {
    three() {
        return console.log(3);
    }
}
const four = new Function('return console.log(4)');

// general
one();

// by call() or apply()
one.call();

// In object
obj2.two();
obj3.three();

// A function constructor
four();
1
1
2
3
4

Functions are objects

It is something that is not very common in other languages.

In JavaScript, I can add a new value in function.

function a() {
    console.log("ok");
}
a.hi = "hello";
console.log(a.hi);
hi

When we create a function, JavaScript create a special type of object is called a callable object.

Leave a Reply

Your email address will not be published.

ANOTE.DEV