difference between call apply bind in javascript

Solutions on MaxInterview for difference between call apply bind in javascript by the best coders in the world

showing results for - "difference between call apply bind in javascript"
Lianne
03 Jun 2017
1// call method calls a function with provided this reference and arguments
2var person = {
3  name: 'Default',
4  fullName: function(city, country) {
5    return this.firstName + " " + this.lastName + " lives in " + city + ", " + country
6  }
7};
8
9person1 = {
10  firstName: 'Jerry',
11  lastName: 'Seinfeld',
12};
13
14person2 = {
15  firstName: 'Michael',
16  lastName: 'Scott'
17};
18
19/**
20 * call and apply takes this reference of any object, with
21 * a difference
22 * [a]pply takes (a for array of arguments)
23 * [c]all takes (c for comma separated arguments)
24 * bind on the other hand also takes this reference of any object
25 * but returns a new function that can be called.
26 * bind also takes comma separated arguments or the arguments can
27 * be passed to the returned function.
28 * Examples:
29 */
30console.log("--apply--");
31console.log(person.fullName.apply(person1, ["New York", "USA"]));
32console.log(person.fullName.apply(person2, ["Scranton", "USA"]));
33console.log("--call--");
34console.log(person.fullName.call(person1, "New York", "USA"));
35console.log(person.fullName.call(person2, "Scranton", "USA"));
36console.log("--bind--");
37var fullNameFunction1 = person.fullName.bind(person1);
38var fullNameFunction2 = person.fullName.bind(person2);
39
40console.log(fullNameFunction1);
41console.log(fullNameFunction2);
42
43console.log(fullNameFunction1 === fullNameFunction2);
44
45console.log(fullNameFunction1("New York", "USA"));
46console.log(fullNameFunction1("Scranton", "USA"));
47console.log("--bind_no_args--");
48var fullNameFunction1NoArgs = person.fullName.bind(person1, "New York", "USA");
49var fullNameFunction2NoArgs = person.fullName.bind(person2, "Scranton", "USA");
50console.log(fullNameFunction1NoArgs());
51console.log(fullNameFunction2NoArgs());
queries leading to this page
differences between call 2c apply and bind in javascriptdifference between call and bind in javascriptwhat is the difference between call bind and apply 3fdifference between call apply bind in javascriptwhat is difference between call and apply and bind in javascriptcall apply bind differencecall apply bind difference in javascriptcall bind apply differencedifference between bind 2c call and applydifference between call apply call and bind methods nodejsdifference between call apply call and bind methodswhat is the difference between call 2c apply and bind 3fdifference between call bind and applycall vs bind vs apply in jscall vs apply vs bind javascriptbind call apply difference javascriptdiff between call bind and applycall vs apply vs bind in javascriptdifferences between bind 2c call and applyjs apply vs call vs bindbind apply call differencediff between call bind and apply and usesdifference between call apply and bindwhat is difference between call apply and binddifference between bind call and apply in javascriptdifference between bind and call in javascriptapply vs call vs bind javascriptwhat is the difference between call apply and bindcall bind and apply differencebind vs call vs apply javascriptwhat is difference between call and apply and bindin javascriptdifference between bind call and applydifference between bind and apply in javascriptdifference call and binddiff between call bind applywhat is the difference between call apply and bind in javascriptdifference between bind call and apply in jaavdifference between call 28 29 apply 28 29 and bind 28 29difference between call and binddifference between call apply bind in javascript