1.实现兼容IE的事件处理程序
//兼容IE的事件处理程序
function addEventListener(ele,type,callback){
if(document.addEventListener){
ele.addEventListener(type,callback);
} else if(document.attachEvent){
function wrapper(){
let event=window.event;
event.target=event.srcElement;
callback.call(ele,event);
}
ele.attachEvent('on'+type,wrapper);
}
}
2.编写深度克隆函数
function clone(obj){
if(typeof obj!=='object' || obj===null){
return obj;
}
if('cloneNode' in obj){
return obj.cloneNode(true);
}
if(Object.prototype.toString.call(obj)==='[object Date]'){
return new Date(obj.getTime());
}
let res=Array.isArray(obj)?[]:
obj.constructor?new obj.constructor():{};
for(let key in obj){
res[key]=clone(obj[key]);
}
return res;
}