jQuery 源码剖析-6 setArray each index 函数说明

119 // Take an array of elements and push it onto the stack

120 // (returning the new matched element set)

121 pushStack: function( elems, name, selector ) {

122    // Build a new jQuery matched element set

123    var ret = jQuery( elems );

 

       // Add the old object onto the stack (as a reference)

126    ret.prevObject = this;

 

128    ret.context = this.context;

 

130    if ( name === "find" )

           ret.selector = this.selector + (this.selector ? " " : "") + selector;

       else if ( name )

           ret.selector = this.selector + "." + name + "(" + selector + ")";

 

       // Return the newly-formed element set

       return ret;

137 },

121 行开始的 pushStack 方法用来将通过数组传递的一个集合,格式化成为一个标准的 jQuery 对象,这样就可以使用 jQuery 提供的方法进行操作了。第 123 将数组转化为一个 jQuery 对象。参见 91 行。

126 行通过 prevObject 属性指向原来的数组对象,128行保存当前的环境引用。

130 行检查是否要在数组上进行进一步的查询,如果提供了查询, 那么更新当前的选择器 selector

136 行,返回构造完成的 jQuery 对象。

 

    // Force the current matched set of elements to become

    // the specified array of elements (destroying the stack in the process)

    // You should use pushStack() in order to do this, but maintain the stack

142 setArray: function( elems ) {

       // Resetting the length to 0, then using the native Array push

       // is a super-fast way to populate an object with array-like properties

       this.length = 0;

       Array.prototype.push.apply( this, elems );

 

       return this;

    },

142 行的 setArray 方法用来将通过参数传递进来的数组,替换掉当前的 jQuery 对象中的查询结果。

 

    // Execute a callback for every element in the matched set.

    // (You can seed the arguments with an array of args, but this is

    // only used internally.)

154 each: function( callback, args ) {

       return jQuery.each( this, callback, args );

    },

each 方法用来遍历查询结果,方法接受一个回调函数,jQuery 将遍历查询结果,对于每一个查询结果对象,调用这个回调函数,并通过参数传递 this 和参数引用。

具体的 each 方法见 671 行函数的定义。

 

671 each: function( object, callback, args ) {

672    var name, i = 0, length = object.length;

673

674    if ( args ) {

           if ( length === undefined ) {

              for ( name in object )

                  if ( callback.apply( object[ name ], args ) === false )

                     break;

           } else

              for ( ; i < length; )

                  if ( callback.apply( object[ i++ ], args ) === false )

                     break;

 

       // A special, fast, case for the most common use of each

       } else {

           if ( length === undefined ) {

              for ( name in object )

                  if ( callback.call( object[ name ], name, object[ name ] ) === false )

                     break;

           } else

              for ( var value = object[0];

                  i < length && callback.call( value, i, value ) !== false; value = object[++i] ){}

       }

 

       return object;

    },

 

 

    // Determine the position of an element within

    // the matched set of elements

160 index: function( elem ) {

       // Locate the position of the desired element

       return jQuery.inArray(

           // If it receives a jQuery object, the first element is used

           elem && elem.jquery ? elem[0] : elem

       , this );

    },

 

160 行的 index 函数用来返回元素在查询结果中的下标。下标从 0 开始,如果没有找到匹配的元素,那么返回 -1

注意:inArray 函数定义在 1086 行。

1086   inArray: function( elem, array ) {

1087       for ( var i = 0, length = array.length; i < length; i++ )

1088       // Use === because on IE, window == document

1089          if ( array[ i ] === elem )

1090              return i;

1091

1092       return -1;

1093   },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值