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

jQuery1.3.2源码学习6:size 和 get 函数

发布时间:2018-09-08 21:48:47 所属栏目:业界 来源:站长网
导读:96 // Start with an empty selector 97 selector: , 98 99 // The current version of jQuery being used 100 jquery: 1.3.2, 101 102 // The number of elements contained in the matched element set 103 size: function() { 104 return this.length; 1
96 // Start with an empty selector
97 selector: "",
98
99 // The current version of jQuery being used
100 jquery: "1.3.2",
101
102 // The number of elements contained in the matched element set
103 size: function() {
104 return this.length;
105 },
106
107 // Get the Nth element in the matched element set OR
108 // Get the whole matched element set as a clean array
109 get: function( num ) {
110 return num === undefined ?
111
112 // Return a 'clean' array
113 Array.prototype.slice.call( this ) :
114
115 // Return just the object
116 this[ num ];
117 },

从 97 行开始,一向到 538 行,界说了 jQuery 工具所共享的要领和属性。

97 行在工具上界说了一个属性 selector,用来暗示当前行使的选择器
110 行在工具上界说了一个属性 jquery,暗示当前的 jQuery 版本
103 行界说了 jQuery 工具的 size 要领,因为 jQuery 工具就是一个仿数组的工具,以是直接返回工具的 length 属性,就是查询功效中的工具数目。

109 行到 117 行,界说了 get 要领,用来返回指定下标的查询工具。
个中 110 行用来判定是否转达了下标参数,假如转达了下标参数,那么参数 num 就不便是 undefined,留意,这里行使了 === 用来判定是否完全相称。
假如转达了参数,那么在 116 行通过属性索引器 [] 来获取指定下标的工具。
假如没有转达参数,那么行使第 113 行处理赏罚。

113 行起首通过 Array.prototype 找到 Array 的原型工具,由于数组的要领都是通过这个原型工具提供的。然后,挪用这个原型工具上的 slice 要领,挪用这个要领的时辰还行使了 call,用来将第一个参数作为要领的 this 参数传入,也就是取适合前查询功效工具上,数组的 slice 可以转达两个参数,第一个参数暗示起始下标,第二个参数暗示竣事下标,假如两个都没有提供,那么起始下标就是 0, 竣事下标就是 length。Slice 将返回从起始下标到竣事下标 – 1 的全部属性,并将功效封装为一个新的真正的数组工具。

(编辑:河北网)

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

    热点阅读