Sizzle
模块第二篇。
顺序看下去,看模块所用的方法。
2、createCache()
方法,创建一个受限的键值对类型缓存。返回的是一个函数,
function createCache(){
var keys = [];
function cache( key, value ){
//向数组`keys`末尾添加元素时,返回新的长度。
if( keys.push( key + ' ' ) > Expr.cacheLength ){
//新的数组长度大于Expr.cacheLength时,删除数组当前首位的元素在对象cache中的缓存
delete cache[ keys.shift() ];
}
//返回 缓存后的对象
return ( cache[ key + ' ' ] = value );
}
//返回缓存方法
return cache;
}
返回的cache
函数,个中实现解释:Expr.cacheLength
就自定义的长度,默认值为50
。
3、markFunction()
标记一个特定的函数
function markFunction( fn ){
fn[ expando ] = true;
return fn;
}
为fn
函数添加一个唯一值的参数并返回。expando
是一个字符串,expando = "sizzle" + 1 * new Date()
。
4、assert()
方法,检测所用元素是否可用。看assert这个单词的含义即知,是一个断言方法。该方法主要用在检测元素是否可用方面。
function assert( fn ){
var el = document.createElement( 'fieldest' );
try {
return !!fn(el);
} catch(e) {
return false;
} finally { // try catch语句无论有无异常都执行finally
//从默认父节点删除自身
if( el.parentNode ){
el.parentNode.removeChild( el );
}
//在IE中释放内存 -- 兼容性
el = null;
}
}
Sizzle
模块第三篇。
5、addHandle()
方法:为所有指定的属性添加处理函数
function addHandle( attrs, handle ){
var arr = attrs.split("|"), //将字符串attrs根据符号|分割成数组
i = arr.length;
while( i-- ){
Expr.attrHandle[ arr[i] ] = handler; //将handler函数添加到attrHandle对象的arr[i]属性中
}
}
喜欢本文请扫下方二维码,关注微信公众号: 前端小二,查看更多我写的文章哦,多谢支持。