冠军

导航

jQuery1.3.2 源码学习-7 setArray,each 函数

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;

679         } else

680             for ( ; i < length; )

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

                        break;

 

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

685     } else {

686         if ( length === undefined ) {

687             for ( name in object )

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

                        break;

690         } else

691             for ( var value = object[0];

692                 i < length && callback.call( value, i, value ) !== false;

value = object[++i] ){}

693     }

 

695     return object;

696 },

Each 的第一个参数为一个 jQuery 对象,第二个参数为处理每一个对象的回调函数,第三个参数可以为回调函数提供一个额外的参数。

注意,如果回调函返回假,那么结束遍历过程。

 

674 行判断是否提供了额外的参数,如果没有提供,那么,执行 685 else 语句部分的常用遍历处理。

686 行检查对象是否提供了 length 属性,如果没有,那么 687 行通过 for in 语句直接遍历对象的所有成员,对于每一个属性,使用 callback 进行处理,为了将当前的对象作为 this 传递给回调函数,使用了 call 的调用方式。

691 行和 692 行处理有 length 属性的情况,直接通过长度进行遍历。

如果有参数数组传递进来,那么,转到 675 行进行处理。

检查对象是否有 length 属性,如果没有使用 for in 语句,如果有,使用下标进行遍历。

680 行处理有 length 属性的情况,这时,直接通过数组的长度进行遍历,由于参数使用了数组方式,所以通过 apply 将当前对象作为 this,其他参数作为数组进行调用。

 

 

posted on 2010-04-23 15:37  冠军  阅读(1498)  评论(1编辑  收藏  举报