Home > Web Front-end > JS Tutorial > js function encapsulation

js function encapsulation

不言
Release: 2018-05-19 10:16:08
Original
2607 people have browsed it

This article shares with you the js function encapsulation. Friends who are interested can take a look

see them...

// 获取网址的get参数var GET = function(name) {  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");  var r = window.location.search.substr(1).match(reg);  if (r != null) return (r[2]);  return null;
}
Copy after login
// 13位时间戳转日期var getLocalTime = function(nS) {  //return new Date(parseInt(nS)).toLocaleString().replace(/:\d{1,2}$/,' ');
  var date = new Date(nS);  var Y = date.getFullYear() + '/';  var M = (date.getMonth() + 1 < 10 ? &#39;0&#39; + (date.getMonth() + 1) : date.getMonth() + 1) + &#39;/&#39;;  var D = (date.getDate() < 10 ? &#39;0&#39; + (date.getDate()) : date.getDate()) + &#39; &#39;;  var h = date.getHours() + &#39;:&#39;;  var m = (date.getMinutes() < 10 ? &#39;0&#39; + (date.getMinutes()) : date.getMinutes()) + &#39; &#39;;  //var s = date.getSeconds();
  return Y + M + D + h + m;
}
Copy after login
// 数组对象排序  data.sort(keysrt("firstWord"));var keysrt = function(propertyName) {  return function(object1, object2) {    var value1 = object1[propertyName];    var value2 = object2[propertyName];    if (value2 < value1) {      return 1;
    } else if (value2 > value1) {      return -1;
    } else {      return 0;
    }
  }
}
Copy after login
// 判断数据类型function type(elem) {  if (elem == null) {    return elem + '';
  }  return toString.call(elem).replace(/[\[\]]/g, '').split(' ')[1].toLowerCase();
}
Copy after login
// 判断数组元素是否重复isArrRepeat(arr) {  var _arr = arr.sort();
  console.log(_arr)  for (var i = 0; i < _arr.length; i++) {    if (_arr[i] === _arr[i + 1]) {      return true;
    }
  }  return false;
}
Copy after login

Related recommendations:

JS encapsulation tool class implements detailed explanation of extracting the first letters of Chinese Pinyin


The above is the detailed content of js function encapsulation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template