微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

自己用的JS函数片段(1)

Number.prototype.clamp = function(min, max) {

return Math.min(Math.max(this, min), max);

};// 补零

String.prototype.padZero = function(length){

var s = this;

while (s.length < length) {

s = '0' + s;

}

return s;

};

Number.prototype.padZero = function(length){

return String(this).padZero(length);

};// 对比数组

Array.prototype.equals = function(array) {

if (!array || this.length !== array.length) {

return false;

}

for (var i = 0; i < this.length; i++) {

if (this[i] instanceof Array && array[i] instanceof Array) {

if (!this[i].equals(array[i])) {

return false;

}

} else if (this[i] !== array[i]) {

return false;

}

}

return true;

};// 0到max-1随机

Math.randomInt = function(max) {

return Math.floor(max * Math.random());

};// 判断是否是移动设备

isMobileDevice = function() {

var r = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i;

return !!navigator.userAgent.match(r);

};// 判断是否是苹果浏览器

isMobileSafari = function() {

var agent = navigator.userAgent;

return !!(agent.match(/iPhone|iPad|iPod/) && agent.match(/AppleWebKit/) &&

!agent.match('CriOS'));

};

// 判断是否是安卓chrome

isAndroidChrome = function() {

var agent = navigator.userAgent;

return !!(agent.match(/Android/) && agent.match(/Chrome/));

};

原文地址:https://www.toutiao.com/article/6823318306225652236/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐