0

This may be considered as a silly question.But i have some confusion about that in my mind,that's why i am about to ask the question here.In this site here.I find this site very informative.There are some definition for callback function described there which are

1.can be passed to another function as parameter

2.can be used as clousers.

3.can be accessed at any point inside the container function .

But there was another definition for callback which is :

It is important to note that the callback function is not executed immediately. It is “called back” (hence the name) at some specified point inside the containing function’s body

which fits for any click events.like document.getElementById('id').onclick=function(){};

But how it fits for array methods like forEach ,map,filter,some and every.It seems like that they invoked immediately during runtime.And i can't use them at anywhere i want according to definition 3. I don't know if they can be used as clousers as i am not an expert in javascript .Still trying to learn.So how it fits for the definitions for callback in javascript.

1 Answer 1

2

A wouldn't regard those statements as a definition. If you're looking for a definition, check you favourite computer science book. Or Wikipedia:

A callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time.

The article only stated that the particular use, a callback as an onclick event listener, is not being executed immediately. That doesn't hold for all callbacks. Wikipedia goes on:

The invocation may be immediate as in a synchronous callback or it might happen at later time, as in an asynchronous callback.

Not all callbacks in JavaScript are asynchronous (see also these questions). However, they are typically associated with asynchrony, as all asynchronous tasks in JavaScript are using a callback.

And i can't use them at anywhere i want according to definition 3.

You don't. map, filter etc. - the "container functions" - do however. They are calling them from whereever they want in their loop.

I don't know if they can be used as clousers

Yes. All JavaScript functions are closures - they can access their parent scope regardless from where they are called.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.