JavaScript
  • JavaScript Introduction
  • JS Engine
  • V8 Engine
  • First-class function
  • Optimized Code
  • Call Stack & Memory heap
  • Single Thread
  • JavaScript RunTime
  • Nodejs
  • Context and Environment
  • Hoisting
  • Functions
  • Arguments
  • Variables
  • Scope
  • IIFE
  • this
  • call(), apply() and bind()
  • currying
  • Types
  • Type Coercion
  • Functions as Object
  • HOF (Higher Order Function)
  • Two pillars of Javascript
  • Closures
  • Prototypal Inheritance
  • OOP and FP
  • OOP
    • 4 principles of OOP
  • FP
    • Pure function
    • Imperative vs Declarative
    • Immutability
    • HOF and Closures
    • Currying
    • Partial Application
    • Compose and Pipe
  • Composition vs Inheritance
  • OOP vs FP
  • JS working
  • Promises
  • Async Await
  • ES5 - ECMAScript 2009
  • ES6 - ECMAScript 2015
  • ES7 - ECMAScript 2016
  • ES8 - ECMAScript 2017
  • ES9 - ECMAScript 2018
  • ES10 - ECMAScript 2019
  • ES11 - ECMAScript 2020
  • ES12 - ECMAScript 2021
  • JOB Queue
  • Promises Execution
Powered by GitBook
On this page

Was this helpful?

Functions as Object

Function is a special type of object in JavaScript.

  • The constructor of the function is an Object.

Ex 1:
function welcome() {
    console.log("Wlecome"); // Welcome
}

welcome.greeting = "Hello";

Ex 2:
const obj = {
    greet() {
        console.log("Hello");
    }
};

obj.greet(); // Hello

In the above example 1 we declared a function welcome. We can print "Welcome" by calling as welcome();. Instead we can also use call() and apply() methods to call the same function. We know that Object only has the key values. Now clearly understood that function is also an Object.

// under the hood of Ex 1
const welcome = {
    name: 'welcome',
    greeting: 'Hello',
    (): console.log("Wlecome")
}

PreviousType CoercionNextHOF (Higher Order Function)

Last updated 5 years ago

Was this helpful?