Functions
Functions are Objects
- In JavaScript functions are objects
function multiply(a, b) {
return a * b;
}
console.log(multiply.name);
console.log(multiply.toString());
console.log(multiply.constructor);
Function Constructor
- When you declare a function JavaScript engine will use function constructor to create the object.
Example-1
function multiply(a, b) {
return a * b;
}
const multiply = new Function("a", "b", "return a * b");
Example-2
function Circle(radius) {
this.radius = radius;
this.draw = function() {
console.log("draw");
}
}
const Circle = new Function("radius", `this.radius = radius;
this.draw = function() {
console.log("draw");
}`)
call, apply, bind
call
,apply
, andbind
are JavaScript methods that allow a single function to be used on multiple objects.- Every time a function is called in JavaScript, it looks for its execution context. Javascript’s
this
keyword points to and returns that execution context.
References: https://betterprogramming.pub/how-and-why-to-use-call-apply-and-bind-in-javascript-17d50d5a1eb0