Zerho
Zerho

Reputation: 1460

Difference between straight calling a method and using the '.call' method ? JS

What is the difference in javascript of calling a method this way

_callback.call(this, "mystring");

instead of

_callback("mystring");

edit: if I am not in need of a context, or i'm fine with the one i'm in, i might say it's useless to do it?

Upvotes: 1

Views: 113

Answers (2)

Ankit Tyagi
Ankit Tyagi

Reputation: 2375

you can set the context when you call a function with 'call' i.e inside function 'this' refer to which object.

For further details check this link :

http://hangar.runway7.net/javascript/difference-call-apply

Upvotes: 1

Quentin
Quentin

Reputation: 944441

call allows you to specify a different value for this inside the function being called.

The first example is called in the context of whatever this is in the function it is being called from.

The second example is called in the context of the default object (in a browser that will be window, or undefined in strict mode).

Upvotes: 2

Related Questions