手写代码之jquery
文章目录
考虑插件和扩展性
class jQuery{
constructor(selector){
var result=document.querySelectorAll(selector);
var length=result.length;
for(let i=0;i<length;i++){
this[i]=result[i];
}
this.length=length;
this.selector=selector;
}
get(index){
return this[index];
}
each(fn){
for(let i=0;i<this.length;i++){
const ele=this[i];
fn(ele);
}
}
on(type,fn){
for(let i=0;i<this.length;i++){
this[i].addEventListener(type,fn,false);
}
}
}
// 插件
jQuery.prototype.dialog=function(info){
alert(info);
}
// 扩展性
class myjQuery extends jQuery{
constructor(selector){
super(selector);
}
addClass(className){
}
style(data){
}
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论