Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The reason to use JS .call() method?

I'm interested what's the reason to have call() method in JS. It seems it duplicates usual method of calling this.

For example, I have a code with call().

var obj = {     objType: "Dog" }  f = function(did_what, what) {     alert(this.objType + " " + did_what + " " + what); }  f.call(obj, "ate", "food"); 

The output is "Dog ate food". But the same result I can get assigning the function to the object.

var obj = {     objType: "Dog" }  f = function(did_what, what) {     alert(this.objType + " " + did_what + " " + what); }  obj.a = f; obj.a("ate", "food"); 

The result is the same. But this way is more understandable and convenient to use. Why call() is needed?

like image 527
Green Avatar asked Jan 25 '12 11:01

Green


People also ask

Why we use call method in JS?

The call() allows for a function/method belonging to one object to be assigned and called for a different object. call() provides a new value of this to the function/method. With call() , you can write a method once and then inherit it in another object, without having to rewrite the method for the new object.

Why do we use call method?

The call() method allows function calls belonging to one object to be assigned and it is called for a different object. It provides a new value of this to the function. The call() method allows you to write a method once and allows it for inheritance in another object, without rewriting the method for the new object.

Why do we need call apply and bind in JavaScript?

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.

What is the difference between call and apply method in JavaScript?

The Difference Between call() and apply() The difference is: The call() method takes arguments separately. The apply() method takes arguments as an array. The apply() method is very handy if you want to use an array instead of an argument list.


2 Answers

call is used when you want to control the scope that will be used in the function called. You might want the this keyword to be something else than the scope you assigned the function to, in those cases you can use call or apply to call the function with your own scope.

F.ex, it also allows you to call utility methods outside the scope, like when using "private" functions:

var obj = (function() {     var privateFn = function() {         alert(this.id);     }     return {         id: 123,         publicFn: function() {             privateFn.call(this);         }     }; }());  obj.publicFn(); 

In the example above, privateFn is not exposed in obj but it can still be constructed as if it was a part of the public scope (using this in the same way).

like image 174
David Hellsing Avatar answered Sep 21 '22 17:09

David Hellsing


2017 Update

All functions by way of Function.prototype have the .call method. The reason to use .call() is to specify what the variable "this" refers to.

MDN specifies:

The call() method calls a function with a given this value and arguments provided individually.

Consider the following:

function x() {     return this; }  x() 

In strict mode x() returns undefined in non strict mode it returns the Global object, Window in a browser context.

Example with .call() we tell it what "this" refers to:

function x() {     return this; }  var obj = {     myName      : 'Robert',     myLocation  : 'Earth' }  x.call(obj); 

Result: {myName: "Robert", myLocation: "Earth"}. In the above example we are specifying the obj object as the value of this inside the function x()

It can be used to emulate inheritance in OOP.

Example:

var Robert = {     name: "Robert Rocha",     age: 12,     height: "5,1",     sex: "male",     describe: function() {         return "This is me " + this.name + " " + this.age + " " + this.height + " " + this.sex;     } }; 

Lets say that the above is a master object(prototype) and you want to inherit the function describe in another object:

var Richard = {     name: "Richard Sash",     age: 25,     height: "6,4",     sex: "male", } 

The Richard object does not have the describe function and you want to simply inherit ,so to speak, the function. You would do it like so:

console.log( Robert.describe.call( Richard ) ); 

Output: This is me Richard Sash 25 6,4 male

like image 30
Robert Rocha Avatar answered Sep 20 '22 17:09

Robert Rocha