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")
}
Last updated
Was this helpful?