https://github.com/baidu-ife/ife/tree/master/2015_spring/task/task0002
深度克隆对像:
function clone(obj){
var o
if(typeof obj=='object'){
if(obj==null){
o=null;
}
if(Object.prototype.toString.call(obj)=='[object Array]'){
for(var i in obj){
o.push(i);
}
}else{
o={};
for(j in obj){
o[j]=clone(ojb[j]);
}
}
}else{
o=obj;
}
}
数据去重
function uniqArr(arr){
var returnarr=[];
var o={};
for(var i in arr){
//
if(typeof o[arr[i]]=='undefined'){
o[arr[i]]='1';
returnarr.push(arr[i]);
// console.log(o[i]);
}
console.log(o);
}
return returnarr;
}
var t=[1,2,2,3,'3'];
console.log(uniqArr(t));
去空格:
function trim(str) {
return str.replace(/^s+|s+$/g,'');
}
console.log(trim(" aaa bb").length);
遍历数组
function each(arr,callback){
for(var i=0;i<arr.length;i++){
callback(i,arr[i]);
}
}
if(typeof Array.prototype.each=='undefined'){
Array.prototype.each=function(callback){
for(var i=0;i<this.length;i++){
callback(i,this[i]);
}
}
}
function addEvent(element,eventname,fn){
if(window.addEventListener){
element.addEventListener(eventname,
fn,false);
}else if(window.attachEvent){
element.attachEvent('on'+eventname,
fn);
}else{
element['on'+eventname]=fn;
}
}
function isIE(){
var userAgent=navigator.userAgent.toLowerCase();
var s;
s=ua.match(/msie ([d.]+)/))
}
function setCookie(c_name,value,expireddays){
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays)
document.cooike=c_name+'='+escape(value)+
((expiredays==null)?'':"expires="+exdate.toGMTString());
}
function getCookie(name){
var arr,reg=new RegExp('(^| )'+name+
'=([^;]*)(;|$)';
if(arr=document.cookie.match(reg)){
return unescape(arr[2]);
}else{
return null;
}
}
var ua=navigator.userAgent.toLowerCase();
return{
VERSION:ua.match(/(msie|webkit|opera|firefox)
[/:/s](d+)/)?RegExp.$2:'0'
}
封装xhr;
var factory={
create:function(){
return function(){
this.init.apply(this,arguments);
}
}
}
factory.prototype={
init:function(successCallback,failCallback){
this.xhr=this.createXMLHttpRequest();
vat tempxhr=this.xhr;
var successFunc=successCallback||null;
var failFunc=failCallback||null;
this.get.apply(this,arguments);
this.post.apply(this,arguments);
this.xhr.onreadystatechange=functio(){
if(tempxhr.readyState){
if(tempxhr.status==200){
successFunc(tempxhr.responseText,
tempxhr.responseXML);
}else{
failFunc(tempxhr.status)
}
}
}
},
get:function(async){
this.xhr.open("GET",this.url,async||false);
this.xhr.send();
},
post:function(data,asnyc){
this.xhr.open("POST",this.url,async||false);
this.xhr.sendRequestHeader("Content-type", "application/x-www-form-urlencoded");
this.xhr.send(data);
},
createXMLHttpRequest:function(){
if(window.XMLHttpRequest){
return new XMLHttpRequest();
}else{
return new ActiveXObject("Microsoft.XMLHTTP");
}
},
random:function(len){
var s=['a','b','c','d','e','f','g','h','i'];
var str;
for(var i=0,i<len;i++){
str=str+s[Math.random()*len];
}
}
}