参数种子的含义是什么?

时间:2013-07-09 11:50:12

标签: javascript sizzle

我正在阅读Sizzle源代码。我看到了下面的定义

function Sizzle(selector, context, results, seed)

我的问题是参数种子的含义是什么?我在API文档中找不到它

由于

附录

seed参数用于jQuery的事件处理程序源(来自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 });
                    }
                }
            }
        }
    },

2 个答案:

答案 0 :(得分:1)

您可以使用seed参数将选择限制为候选列表。只需传入一组DOM元素。

例如,假设我们有以下DOM:

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

然后,执行以下选择:

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

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

可是:

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

注意:此功能似乎不属于公共API。

答案 1 :(得分:0)

种子通常用于确定伪随机数的特定序列。如果您想在每次运行中使用相同的重复数字顺序,则使用相同的种子。随机数生成器可以使用时间戳来确保种子变化,但是为了测试它,能够设置这样的种子非常有用。

我认为这种情况下的种子具有相似的含义,如果种子相同,则意味着Sizzle的结果在每次运行中都是相同的,如果结果不同,结果会有所不同。

相关问题