0

How does anonymous function know what parameters are being passed in? For example,

Case 1:

$( "li" ).each(function( index ) {
  console.log( index + ": " + $( this ).text() );
});

Case 2:

$( "li" ).each(function(e) {
  e.preventDefault();
});

Case 3:

$( "li" ).each(function( index, element ) {
  console.log( index + ": " + element + $( this ).text() );
});

In each of these cases, how does the anonymous function know what e, index, element are?

2
  • 2
    Because .each passes those values to the function. Also note that your second example doesn't work, because .each doesn't pass an event object to the callback. And you can name the parameters however you want to. Commented Feb 9, 2014 at 1:00
  • In all of your cases, the first element will always be the same thing. index = e. In javascript, you don't have to specify the number of parameters you send to a function. You just have to make the function adapt to that number of parameters and each new param will be some precised thing. Then, .each passes the values you specify to the anonymous function. Commented Feb 9, 2014 at 1:01

3 Answers 3

2

Because the code in each will call the function you pass it with the arguments.

function notEach(callback) {
    callback(1,2,3);
}

notEach(function (one, two, three) {
    console.log(one, two three);
});
Sign up to request clarification or add additional context in comments.

9 Comments

yes, but why can't you use index.preventDefault() for example? Sorry I am a noob and I don't get this.
@guest: You are confusing .each with one of the event handling methods, such as .click. See api.jquery.com/each and api.jquery.com/click
@guest — Because it doesn't pass an event object in (which makes sense, because each is used to run some code on each item in an array, not to set up an event handler).
well let's forget about each. I don't care what function I use. I am solely talking about the anonymous function that is inside each.
@guest — We can't forget about each. The arguments a callback function gets called with depend entirely on the thing that is calling it. When you pass a function to on it gets called with an event object (which has a preventDefault method) because you passed it to on, not because it was an anonymous function.
|
0

If we can speak of each as if it's a little robot, each tells the function what its arguments are. It picks up the collection and the function, puts the elements of the collection into the function one at a time, and sticks the outputs together as a collection. http://api.jquery.com/jquery.each/

You should understand that when you write an anonymous function, it's both a procedure and an object. You are simultaneously defining for Javascript how to carry out that function ("knowing how" to call it) and what that function is ("knowing that" it is a particular object). Some languages are still in use where a function is not an object. So maybe that fact was confusing when you encountered it here.

Comments

0

naive implementation of each function:

function $(selector) {

    var elements = document.querySelectorAll(selector);

    return {
        each: function (fn) {
            for (var i = 0; i < elements.length; i++) {
                fn(i, elements[i]);
            }        
        }
    };
}

The jQuery function returns an object with an each function. This function has access to the matched elements and iterates over them. It can call the function you gave it on every iteration and pass the current index and corresponding element to it.

Your second case doesn't make much sense to me. As far as I know jQuery, no event is passed to the each function.

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.