0

i am very new to JS and Jquery and i have a small difficulty understanding a certain function , I.E. i want to understand the order of execution of a very simple function that i found in the jquery documentation , look below (the example can also be found here.)::

HTML ::

<form method="post" action="">
  <fieldset>
    <div>
      <label for="two">2</label>
      <input type="checkbox" value="2" id="two" name="number[]">
    </div>
    <div>
      <label for="four">4</label>
      <input type="checkbox" value="4" id="four" name="number[]">
    </div>
    <div>
      <label for="six">6</label>
      <input type="checkbox" value="6" id="six" name="number[]">
    </div>
    <div>
      <label for="eight">8</label>
      <input type="checkbox" value="8" id="eight" name="number[]">
    </div>
  </fieldset>
</form>

JS ::

$( ":checkbox" )
  .map(function() {
    return this.id;
  })
  .get()
  .join();

The result is ::

 "two,four,six,eight".

i understand what the indivisual functions are doing above , but can somebody tell me whats the order of execution of the above script .I.E.

is map() returning indivisual id's to get() ? or is it looping over all the checkbox's and then passing all the id's at once ?

5
  • map operates on the collection set and returns an array of the results you return in the map. Commented May 20, 2015 at 13:27
  • 1
    In this case, it returns an array of strings (id) Commented May 20, 2015 at 13:28
  • @Taplar , i get what the whole function is doing , my question is , is map() returning indivisual id's to get() ? or is it looping over all the checkbox's and then passing all the id's at once ? , Thanks for your help though ! Commented May 20, 2015 at 13:31
  • As said above, it returns an array. It operates over the entire set, keeping an internal array of the results, and at the end it returns the result array. Commented May 20, 2015 at 13:32
  • map() is returning array like object , then you call on it the get() method to get an array on which you can use array protype methods as join() Commented May 20, 2015 at 13:33

3 Answers 3

2

The functions are executed in turn, once they complete they hand the result onto the next function, etc.

It's the equivalent of:

var first = $( ":checkbox" ).map(function() {
    return this.id;
  });
var second = first.get();
var third = second.join();

the above is just a shorthand that doesn't require the extra varibales.

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

1 Comment

thanks just to reconfirm , only after .map() iterates over all the checkbox's does get() execute(i mean in my version of the code) ?
2

is map() returning indivisual id's to get() ? or is it looping over all the checkbox's and then passing all the id's at once

As per docs for .map(),

Pass each element in the current matched set through a function, producing a new jQuery object containing the return values

Answer: It loops over all the elements available in object and append the id in array at a time. The array generated from .map() is jquery object. .get() converts the object into value array.

1 Comment

But OP is using jQuery. fn.map
0

the order is as you read it , I mean

1-

   $( ":checkbox" )
  .map(function() {
    return this.id;
  })

2- get()

3- join()

2 Comments

Nooooooo :D not that order my question is s map() returning indivisual id's to get() ? or is it looping over all the checkbox's and then passing all the id's at once ?
First query is processed totally, this means loop all checkbox. Then secong query is processed with result of checkbox. Then third

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.