3

I'm reading Sizzle source code. I saw the definition below

function Sizzle(selector, context, results, seed)

My question is what's the meaning about the parameter seed? I can't find it in API document

Thanks

addendum

The seed parameter is used in jQuery's event handler source (from 2.1.4):

jQuery.find = Sizzle;
// [...]
jQuery.event = {
    // [..]
    handlers: function( event, handlers ) {
        // [..]
        // Find delegate handlers
        if ( delegateCount && cur.nodeType && (!event.button || event.type !== "click") ) {

            for ( ; cur !== this; cur = cur.parentNode || this ) {

                // Don't process clicks on disabled elements (#6911, #8165, #11382, #11764)
                if ( cur.disabled !== true || event.type !== "click" ) {
                    matches = [];
                    for ( i = 0; i < delegateCount; i++ ) {
                        handleObj = handlers[ i ];

                        // Don't conflict with Object.prototype properties (#13203)
                        sel = handleObj.selector + " ";

                        if ( matches[ sel ] === undefined ) {
                            matches[ sel ] = handleObj.needsContext ?
                                jQuery( sel, this ).index( cur ) >= 0 :
                                // 
                                // Right here to find if cur matches the 
                                // delegated event handler's selector.
                                // 
                                jQuery.find( sel, this, null, [ cur ] ).length;
                                // There: -----------------------^
                        }
                        if ( matches[ sel ] ) {
                            matches.push( handleObj );
                        }
                    }
                    if ( matches.length ) {
                        handlerQueue.push({ elem: cur, handlers: matches });
                    }
                }
            }
        }
    },
Community
  • 1
  • 1
user2155362
  • 1,657
  • 5
  • 18
  • 30

2 Answers2

1

You can use the seed parameter to limit the selection to a list of candidates. Just pass in an array of DOM elements.

For example let's say we have the following DOM:

<div id="id1"></div>
<div id="id2"></div>

Then, perform the following selections:

Sizzle("#id1", null, null, null);
// [<div id=​"id1">​</div>​]

And:

var candidates = [
    document.getElementById("id1"),
    document.getElementById("id2")
];
Sizzle("#id1", null, null, candidates);
// [<div id=​"id1">​</div>​]

But:

var candidates = [
    document.getElementById("id2")
];
Sizzle("#id1", null, null, candidates);
// []

Note: This functionality doesn't seem to be part of the public API.

EyalAr
  • 3,160
  • 1
  • 22
  • 30
0

A seed is usually used to determine specific sequences of pseudo-random numbers. If you want the same repeated order of numbers on every run you use the same seed. Random number generators can use time stamps to make sure seeds vary, but for testing it it extremely useful to be able to set such seeds.

I assume the seed in this case will have a similar meaning, it will mean the outcome of Sizzle will be identical on every run if the seed is the same, if it is different the outcomes will be different.

Jon Taylor
  • 7,865
  • 5
  • 30
  • 55
  • Hi Jon Taylor, thanks for your replay. Can you give me some example? – user2155362 Jul 09 '13 at 11:56
  • Well I just looked at the API and I have no idea where you are getting this seed parameter from, it doesn't exist in their official docs so I cant tell you what it does. The public API only shows the function without the seed param. As for examples, just try running the function with the same seed multiple times and observe the result. Then change the seed and see what happens. – Jon Taylor Jul 09 '13 at 12:03
  • Your explanation is true in a general sense, but EyalAr provides the correct answer for Sizzle. – bernie Oct 28 '15 at 18:49