1. 常用的方法:
var ExplorerUtil = {
getExplorer: function(){
var explorer = window.navigator.userAgent;
console.log(explorer);
//ie
if (explorer.indexOf("Trident") >= 0) {
return 'IE';
}
//firefox
else if (explorer.indexOf("Firefox") >= 0) {
return 'Firefox';
}
//Chrome
else if(explorer.indexOf("Chrome") >= 0){
return 'Chrome';
}
//Opera
else if(explorer.indexOf("Opera") >= 0){
return 'Opera';
}
//Safari
else if(explorer.indexOf("Safari") >= 0){
return 'Safari';
}
},
isIE: function(){
return 'IE' == this.getExplorer();
}
};
var DateUtil = {
format: function(date) {
var y = date.getFullYear();
var m = date.getMonth()+1;
var d = date.getDate();
return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
},
parse: function(s) {
if (!s) return new Date();
var ss = (s.split('-'));
var y = parseInt(ss[0],10);
var m = parseInt(ss[1],10);
var d = parseInt(ss[2],10);
if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
return new Date(y,m-1,d);
} else {
return new Date();
}
}
}
var StringUtil = {
// Join string for GET request
objToString: function(data, isPrefix) {
var prefix = isPrefix ? '?' : '',
result = [];
var nulls = ['', undefined, null];
for (var key in data) {
var value = data[key];
if (!ExplorerUtil.isIE() ? nulls.includes(value) : nulls.indexOf(value) > -1) {
continue;
}
if (value.constructor === Array) {
value.forEach(function(val, key){
result.push(encodeURIComponent(key) + '[]=' + encodeURIComponent(val));
});
} else {
result.push(encodeURIComponent(key) + '=' + encodeURIComponent(value))
}
}
return result.length ? prefix + result.join('&') : '';
}
}
var NumberUtil = {
parseToInt: function(s){
return ExplorerUtil.isIE() ? Number(s) : Number.parseInt(s);
},
parseToFloat: function(s){
return ExplorerUtil.isIE() ? Number(s) : Number.parseFloat(s);
}
}
博客提及了JavaScript常用的方法,但未详细展开。JavaScript是前端开发重要语言,其常用方法在前端开发中应用广泛。
371

被折叠的 条评论
为什么被折叠?



