Jump to content

call method


jimfog
 Share

Recommended Posts

I found here info about the call method here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call

 

I assume you already know that it is a way to call a function.

 

The question I want to make is if there any point in using it instead of the "traditional" way to do it-namely writing the function name?

Link to comment
Share on other sites

It's one way to call a function where you can specify the scope in which the function runs.

If it is not much of a trouble can you give an example?

 

From the way you put it I assume that with the "traditional way" you cannot specify the scope-am I correct?

Link to comment
Share on other sites

You normally specify a function's context when it's created. The context of a function depends on where and how it was created.

 

If the function was created in the global scope "this" refers to the window:

function a() {}

If the function belongs to an object, "this" refers to the object:

var obj = {};obj.a = function() { }

setTimeout() and setInterval() are some of the methods that take the function out of its context, so those are some of the cases where Function.call() or Function.apply() are used to make corrections.

 

Unless you find youself in one of those kinds of situations you should just call the function the ordinary way.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...