一些常用的JavaScript函數(json)附詳細說明

來源:互聯網
上載者:User

複製代碼 代碼如下:var YX = {
//得到JS內建資料類型的類型,傳回值包括[Date,RegExp,Number,String,Array,Boolean,Function,null,undefined,Object]
getType : function(obj){
return obj == null ? obj + "" : Object.prototype.toString.call(obj).slice(8,-1);
}
//建立一個簡單的類
,createClass : function(){
return function(){
this.init.apply(this,arguments);
};
}
//格式化字串,YX.format("{0},{1},haha",["hello","world"])
,format : function(str,params){
var reg = /{(\d+)}/g;
return str.replace(reg,function(match,val){
return params[~~val];
});
}
//格式化字串,YX.format2("數學={數學},語文={語文},haha",{"數學":100,"語文":99})
,format2 : function(str,params){
var reg = /{([^{}]+)}/g;
return str.replace(reg,function(match,val){
return params[val];
});
}
//格式化時間,YX.format3(new Date,"yy-mm-dd H:M:S")
,format3 : function(date,patten){
var y = date.getFullYear(),mon = date.getMonth() + 1,d = date.getDate(),h = date.getHours(),min = date.getMinutes(),s = date.getSeconds()
,zero = function(o){return ("0"+o).slice(-2)}
,matchs = {"yy":y,"y":(y+"").slice(-2),"mm":zero(mon),"m":mon,"dd":zero(d),"d":d,"HH":zero(h),"H":h,"MM":zero(min),"M":min,"SS":zero(s),"S":s};
return patten.replace(/yy|y|mm|m|dd|d|HH|H|MM|M|SS|S/g,function(match){
return matchs[match];
});
}
//數組去重,YX.unique([1,1,"1",document.body,document.body]
,unique : function(arr){
var kv = {},len = arr.length,rs = [],t;
for(;len--;){
t = arr[len];
(kv[t]==undefined || kv[t]!==t) && (kv[t]=t,rs.push(t));
}
return rs;
}
};
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.