Home >Web Front-end >JS Tutorial >Some commonly used JavaScript functions (json) with detailed description_javascript skills

Some commonly used JavaScript functions (json) with detailed description_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 18:06:141110browse
Copy code The code is as follows:

var YX = {
//Get the JS built-in data type Type, return value includes [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);
}
//Create a simple class
, createClass : function(){
return function(){
this.init.apply(this,arguments);
};
}
//Format string, 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];
});
}
//Format string, YX.format2("Mathematics={Mathematics}, Chinese={Chinese },haha",{"Mathematics":100,"Chinese":99})
,format2 : function(str,params){
var reg = /{([^{}] )}/g ;
return str.replace(reg,function(match,val){
return params[val];
});
}
//Formatting time, 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 matches[match];
});
}
//Array deduplication, 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;
}
};
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn