declaring a variable without returning a value .

Alexander Solonik

hey guys i am new to Jquery and JS in general and have a very simple question basically i came across a sorting script online and i tried to understand whats going on by going through the jquery documentation :

the script is as follows :

$(function () {
        var buttons = $('#fruit,#vegetable,#meat').click(function() {
            $(this).toggleClass('active');
            var classes = buttons.filter('.active').map(function() {
                return this.id;
            })
            .get().join(',.');
            $('div.fruit,div.vegetable,div.meat').hide().
                filter('.' + (classes || 'none')).show();               
                console.log(classes.get().join(',.'));
        });
    });

and this script is run on the following HTML :

<div style="float:right; padding:25px;">
            <button id="fruit" class="active"><span>fruit</span></button>
            <button id="vegetable" class="active">vegetable</button>
            <button id="meat" class="active">meat</button>
        </div>

        <div>
            <p>Trying to use buttons as an "or" case rather than "and." When choosing fuit or vegetable, I want to see tomato as part of each list, <em>not just</em> when both are selected.</p>  

        <div class="fruit">
            <p>apple</p>
        </div>

        <div class="vegetable">
            <p>pumpkin</p>
        </div>

        <div class="vegetable">
           <p>okra</p>
        </div>

        <div class="fruit">
            <p>orange</p>
        </div>

        <div class="meat">
            <p>beef</p>
        </div>    


        <div class="fruit vegetable">
            <p>tomato</p>
        </div>    

        </div>

fiddle here.

now i understand most part of the script except the following :

on line four of the script the author is using the following code :

                var classes = buttons.filter('.active').map(function() {

now button has not been returned any value , so should't it be empty ??

or is it that by saying the following (on line one):

            var buttons = $('#fruit,#vegetable,#meat').click(function() {

the author has also defined the button implicitly , as in :

var buttons = $('#fruit,#vegetable,#meat')

can somebody clarify this for me , as i am struggling to grasp this simple concept .

Thank you.

Alex-Z.

Alnitak

It's an unusual pattern, but buttons is indeed declared correctly (and explicitly`) in the statement reading:

var buttons = $(...).click(...);

By the time any click event ever happens the above code will have executed (since JS is single-threaded) and buttons will already contain the desired set of elements and can be safely used within the callback.

This also of course relies on the way that many jQuery methods "chain" - they return the same object as the one on which they were called, so the net result is that buttons does just contain $('#fruit,#vegetable,#meat')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

declaring a variable without returning a value .

From Dev

bash - declaring a variable without assigning a value

From Dev

Scala shorthand for declaring and returning a variable

From Dev

Use an array without declaring it in a variable

From Dev

Returning a dictionary value in C# without assigning it to a variable?

From Dev

Difference between declaring a variable with and without get; set;

From Dev

recursion not working without declaring global variable

From Dev

Change button color without declaring member variable

From Dev

Declaring an variable of class type without initializing it

From Dev

Declaring a character variable without quotes in C

From Dev

Returning variable value in sailsJS

From Dev

Return not returning variable value

From Dev

Returning variable value in sailsJS

From Dev

AngularJS declaring variable with initial value in View?

From Dev

Why give a variable a null value when declaring it?

From Dev

Declaring variable type as varchar with default value

From Dev

AngularJS declaring variable with initial value in View?

From Dev

Declaring a variable with a value from select query

From Dev

Function returning a variable without declared it

From Dev

Returning and printing without assigning to variable?

From Dev

Function returning a variable without declared it

From Dev

returning promise to a variable without $$state

From Dev

Smalltalk: Assigning to a Variable Without Declaring a Variable in GNU Smalltalk

From Dev

In Java how to refer subclass variable without declaring that variable in parent class?

From Dev

Is returning a value better than returning a local variable?

From Dev

Mongodb returning value without fieldname

From Dev

Returning dict Value with a variable Key

From Dev

MySQL Variable Returning Incorrect Value

From Dev

Best practice for declaring variable without initialising it, so auto is unavailable

Related Related

  1. 1

    declaring a variable without returning a value .

  2. 2

    bash - declaring a variable without assigning a value

  3. 3

    Scala shorthand for declaring and returning a variable

  4. 4

    Use an array without declaring it in a variable

  5. 5

    Returning a dictionary value in C# without assigning it to a variable?

  6. 6

    Difference between declaring a variable with and without get; set;

  7. 7

    recursion not working without declaring global variable

  8. 8

    Change button color without declaring member variable

  9. 9

    Declaring an variable of class type without initializing it

  10. 10

    Declaring a character variable without quotes in C

  11. 11

    Returning variable value in sailsJS

  12. 12

    Return not returning variable value

  13. 13

    Returning variable value in sailsJS

  14. 14

    AngularJS declaring variable with initial value in View?

  15. 15

    Why give a variable a null value when declaring it?

  16. 16

    Declaring variable type as varchar with default value

  17. 17

    AngularJS declaring variable with initial value in View?

  18. 18

    Declaring a variable with a value from select query

  19. 19

    Function returning a variable without declared it

  20. 20

    Returning and printing without assigning to variable?

  21. 21

    Function returning a variable without declared it

  22. 22

    returning promise to a variable without $$state

  23. 23

    Smalltalk: Assigning to a Variable Without Declaring a Variable in GNU Smalltalk

  24. 24

    In Java how to refer subclass variable without declaring that variable in parent class?

  25. 25

    Is returning a value better than returning a local variable?

  26. 26

    Mongodb returning value without fieldname

  27. 27

    Returning dict Value with a variable Key

  28. 28

    MySQL Variable Returning Incorrect Value

  29. 29

    Best practice for declaring variable without initialising it, so auto is unavailable

HotTag

Archive