加入收藏 | 设为首页 | 会员中心 | 我要投稿 河北网 (https://www.hebeiwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 业界 > 正文

jQuery1.3.2源码进修7:setArray,each 函数

发布时间:2018-09-02 22:24:42 所属栏目:业界 来源:站长网
导读: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 ); //
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,其他参数作为数组举办挪用。

来历:博客园

(编辑:河北网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读