0% found this document useful (0 votes)
122 views2 pages

Nisha Callapplybind

The document discusses the call, apply and bind methods in JavaScript. It provides examples of using call and apply to invoke a function and set the value of this, and using bind to create a bound function. It also demonstrates the difference that bind does not automatically call the function.

Uploaded by

Sambeet Sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views2 pages

Nisha Callapplybind

The document discusses the call, apply and bind methods in JavaScript. It provides examples of using call and apply to invoke a function and set the value of this, and using bind to create a bound function. It also demonstrates the difference that bind does not automatically call the function.

Uploaded by

Sambeet Sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// call, apply and bind

let employee = {
name:"sam",
age:38,
getDetails: function(sex){
console.log(this)
console.log(`${this.name} ${this.age} ${sex}`)
}
}

let customer = {
name:"sus",
age:34
}

// employee.getDetails()
// employee.getDetails.call(customer,"female");
// employee.getDetails.apply(customer,["female"]);
// ----------------------------------
// we can remove the getDetails() from employee object and put it outside

let getDetails = function(sex){


console.log(this)
console.log(`${this.name} ${this.age} ${sex}`)
}
let employee = {
name:"sam",
age:38
}

let customer = {
name:"sus",
age:34
}
getDetails.call(employee)
getDetails.call(customer,"female");
getDetails.apply(customer,["female"]);

// ---------------------------------- bind

let getDetails = function(sex){


console.log(this)
console.log(`${this.name} ${this.age} ${sex}`)
}
let employee = {
name:"sam",
age:38
}

let customer = {
name:"sus",
age:34
}
let x = getDetails.bind(employee);
x("male") // here we pass the arguments in comma not array type

// Import point of bind is it will execute when the user calls it


// It will not automatically call the function like call and apply at time of
binding

// Ex:- we need to create a button and on the click of that details will populate
in console

let getDetails = function(sex){


console.log(this)
console.log(`${this.name} ${this.age} ${sex}`)
}
let employee = {
name:"sam",
age:38
}
let customer = {
name:"sus",
age:34
}
let btn = document.getElementById("btn");
btn.addEventListener("click",function(){
getDetails.apply(customer,["male"])
})

You might also like