call apply bind in javascript examples

Answer in SIMPLEST form

Call invokes the function and allows you to pass in arguments one by one.
Apply invokes the function and allows you to pass in arguments as an array.
Bind returns a new function, allowing you to pass in a this array and any number of arguments.

Apply vs. Call vs. Bind Examples
--------------
Call

var person1 = {firstName: 'Jon', lastName: 'Kuperman'};
var person2 = {firstName: 'Kelly', lastName: 'King'};

function say(greeting) {
    console.log(greeting + ' ' + this.firstName + ' ' + this.lastName);
}

say.call(person1, 'Hello'); // Hello Jon Kuperman
say.call(person2, 'Hello'); // Hello Kelly King

--------------
Apply

var person1 = {firstName: 'Jon', lastName: 'Kuperman'};
var person2 = {firstName: 'Kelly', lastName: 'King'};

function say(greeting) {
    console.log(greeting + ' ' + this.firstName + ' ' + this.lastName);
}

say.apply(person1, ['Hello']); // Hello Jon Kuperman
say.apply(person2, ['Hello']); // Hello Kelly King

-------------
Bind

var person1 = {firstName: 'Jon', lastName: 'Kuperman'};
var person2 = {firstName: 'Kelly', lastName: 'King'};

function say() {
    console.log('Hello ' + this.firstName + ' ' + this.lastName);
}

var sayHelloJon = say.bind(person1);
var sayHelloKelly = say.bind(person2);

sayHelloJon(); // Hello Jon Kuperman
sayHelloKelly(); // Hello Kelly King

3.5
2
Enthusiast 95 points

                                    Apply vs. Call vs. Bind Examples

Call

var person1 = {firstName: 'Jon', lastName: 'Kuperman'};
var person2 = {firstName: 'Kelly', lastName: 'King'};

function say(greeting) {
    console.log(greeting + ' ' + this.firstName + ' ' + this.lastName);
}

say.call(person1, 'Hello'); // Hello Jon Kuperman
say.call(person2, 'Hello'); // Hello Kelly King

Apply

var person1 = {firstName: 'Jon', lastName: 'Kuperman'};
var person2 = {firstName: 'Kelly', lastName: 'King'};

function say(greeting) {
    console.log(greeting + ' ' + this.firstName + ' ' + this.lastName);
}

say.apply(person1, ['Hello']); // Hello Jon Kuperman
say.apply(person2, ['Hello']); // Hello Kelly King

Bind

var person1 = {firstName: 'Jon', lastName: 'Kuperman'};
var person2 = {firstName: 'Kelly', lastName: 'King'};

function say() {
    console.log('Hello ' + this.firstName + ' ' + this.lastName);
}

var sayHelloJon = say.bind(person1);
var sayHelloKelly = say.bind(person2);

sayHelloJon(); // Hello Jon Kuperman
sayHelloKelly(); // Hello Kelly King

3.5 (2 Votes)
0
3
1
P J Firth 120 points

                                    const obj = { number: 1 }

function foo() {
	console.log(this.number)
}

//bind - binds obj's 'this' context to foo, but doesn't call it
const newFoo = foo.bind(obj) 

//call/apply - binds obj's 'this' context to foo, then calls it
foo.call(obj, /*arg1*/, /*arg2*/) 
foo.apply(obj, [/*arg1*/, /*arg2*/])

//Only difference between call/apply is argument passing - ',' vs '[]'

3 (1 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
apply/bind on callback where we can use apply call and bind call apply bind javascript javascript this call apply bind apply call bind in javascript educba apply call bind in javascript programiz javascript bind call apply w3schools apply bind & in javascript bind call and apply in js javascript function when to use bind or apply javascript call apply bind mnd this bind call apply call apply and bind method in javascript for beginners Explain call(), apply() and, bind() methods. what's the bind() call(), apply()? call bind and apply methods javascript info call, apply, bind method this bind call apply js bind call apply js javascript call and apply bind call() apply() and bind() in javascript Explain call(), apply() and, bind() methods .call(), .apply(), .bind() call, apply and bind javscript call apply bind in javascript mdn apply bind js apply bind call js bind or apply when should we use bind call apply in javascript bind apply call example javascript what does apply call and bind to ? what does apply call and bind do ? js apply and bind js concept of call apply bind call apply bind example call apply and bind methods in javascript what is bind apply & * in javascript What is call, apply and bind function in JS? understanding call apply and bind what is bind call apply function in javascript implement call apply bind call(), apply() and, bind() methods. call apply bind javascript examples Apply, call, and bind call, bind , apply explain how to use call, apply and bind methods in javascript Implement your own — call(), apply() and bind() method in JavaScript fnciton call apply bind js bind call apply what is call bind and apply in javascript call bind apply js call apply bind js function in dom apply bind call javascript CALL APPLY bind in javascript WITH ARGUMENTS CALL APPLY bind in javascript WITH ARGUMENT javascript bind apply call call apply bind methods in javascript call apply bind definition call apply bind defination call apply bind dow we need when to use call bind and apply call apply bind in js when to use call apply bind call apply bind in js w3 real world use of call() apply() bind() bind, call and apply javascript call apply bind code 11. explain call() apply() and bind() in javascript w3schools call apply and bind in javascript w3schools Call, apply, bind bind apply bind call apply es6 call bind apply javascript mdn call bind apply what is the use of call apply and bind in javascript practicle use what is the use of call apply and bind in javascript bind, call and apply call and apply and bind in javascript what is the different between bind, apply and call in javascript function bind and apply in javascript js bind, call or apply bind apply js call apply bind javascript w3schools What is a practical use for call bind apply in JavaScript? javascript function call apply bind what is call, apply, bind javascript function apply call bind bind call apply with example apply bind javascript call apply bind in javascript msdn how to know where we use call apply and bind method in javascript call bind apply in javascript with example bind apply call javascript call apply and bind in javascript mdn call apply bind in javascript w3schools why we use call apply and bind js apply call bind js bind apply call js function bind call apply what is call apply bind in javascript bind call apply using this in javascript what is call apply bind method in JavaScript bind apply calll javascript bind and apply js bind apply function’s call, apply and bind methods use of call apply and bind in javascript call apply bind in javascript examples this keyword, call, bind, apply in javascript example javascript built in function call apply bind js call bind apply call bind and apply in detail javascript call bind apply javascript bind, call, apply bind call apply javasript javascript call apply bind example call apply bind js What is apply call bind bind and apply javascript bind apply call in javascript what is call apply bind function bind apply call .call .apply .bind call apply and bind call bind and apply call, apply, and bind methods apply bind call apply() call() and bind() methods bind call apply js call apply and bind apply call bind in javascript bind call apply in js example of call bind apply js call apply bind Call, Apply and Bind javascript call bind and apply "call apply bind in javascript" apply call bind call apply and bind in javascript call bind apply statements in javascript call bind apply in javascript Call ,apply ,bind methods in JavaScript function call apply bind call apply bind in javascript bind call apply javascript call bind apply in javascript w3schools call bind apply in js apply call bind js this call apply bind call bind and apply in javascript .bind() js js apply bind call() apply() and bind() apply and bind in javascript how to bind this to a function use call apply bind apply call bind call apply call apply and bind method in javascript call apply and bind javascript apply bind and call in javascript call bind apply method with examples what is call apply and bind in javascript javascript call apply bind bind apply and call bind call and apply in javascript call bind apply bind call apply in javascript javascript bind call apply how to use call, apply and bind apply call in js javascript call() apply() bind() Javascript call() apply() bind() call apply bind
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source