8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

JavaScript常用开发库:对原声JS进行扩展

IT171中文网 管理员组 2013-10-24 1432

  在实际的开发过程中,javascript原生对象的方法不能满足我们的需求,我们需要对原生对象进行一些扩展,以方便我们开发,提高开发效率,避免大量的重复代码。我只扩展了一些方法,一些参考来至互联网以下是常用的,我把代码贴出来。
  Date.prototype.pattern = function (fmt) {
  var o = {
  "M+": this.getMonth() + 1, //月份
  "d+": this.getDate(), //日
  "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时
  "H+": this.getHours(), //小时
  "m+": this.getMinutes(), //分
  "s+": this.getSeconds(), //秒
  "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  "S": this.getMilliseconds() //毫秒
  };
  var week = {
  "0": "\u65e5",
  "1": "\u4e00",
  "2": "\u4e8c",
  "3": "\u4e09",
  "4": "\u56db",
  "5": "\u4e94",
  "6": "\u516d"
  };
  if (/(y+)/.test(fmt)) {
  fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  if (/(E+)/.test(fmt)) {
  fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[this.getDay() + ""]);
  }
  for (var k in o) {
  if (new RegExp("(" + k + ")").test(fmt)) {
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  }
  }
  return fmt;
  };
  Date.prototype.addYears = function (value) {
  this.setFullYear(this.getFullYear() + value);
  return this;
  };
  Date.prototype.addHours = function (value) {
  this.setHours(this.getHours() + value);
  return this;
  };
  Date.prototype.addDays = function (value) {
  this.addHours(24 * value);
  return this;
  };
  Date.prototype.addMonths = function (value) {
  this.setMonth(this.getMonth() + value);
  return this;
  };
  Date.prototype.toDate = function () {
  return this;
  };
  String.prototype.toDateTime = function (dateformat) {
  var date = new Date(parseInt(this.replace("/Date(", "").replace(")/", ""), 10));
  var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  if (dateformat == "yyyy-mm-dd") {
  return date.getFullYear() + "-" + month + "-" + currentDate;
  }
  return date.getFullYear() + "-" + month + "-" + currentDate + " " + hours + ":" + minutes + ":" + seconds;
  };
  String.prototype.toDate = function () {
  //将字符串转换为日期
  if (this.indexOf("Date(") > 0) {
  var str = this.substr(this.indexOf("(") + 1);
  str = str.substr(0, str.indexOf(")"));
  return new Date(Number(str));
  }
  return new Date(Date.parse(this.replace(/-/g, "/")));
  };
  String.prototype.htmlEncode = function () {
  var s = this;
  s = s.replace(/&/g, "&");
  s = s.replace(/
  s = s.replace(/>/g, ">");
  //s = s.replace(/ /g, " ");
  s = s.replace(/'/g, "&apos;");
  s = s.replace(/\"/g, """);
  return s;
  };
  String.prototype.htmlDecode = function () {
  var s = this;
  s = s.replace(/&/g, "&");
  s = s.replace(/
  s = s.replace(/>/g, ">");
  //s = s.replace(/ /g, " ");
  s = s.replace(/'/g, "\'");
  s = s.replace(/"/g, "\"");
  return s;
  };
  ///获取字符串字节长度
  String.prototype.getBytesLength = function () {
  var cArr = this.match(/[^\x00-\xff]/ig);
  return this.length + (cArr == null ? 0 : cArr.length);
  };
  ///字符串截取,hasEllipsis:是否在截取字符串后加上省略号.
  //lengthType:截取字符串长度的类型(1:字节,2:字数)
  String.prototype.CutStrings = function (length, hasEllipsis) {
  var newStr;
  if (this.length <= length)
  newStr = this;
  else
  newStr = this.substr(0, length);
  if (hasEllipsis) {
  newStr += "...";
  }
  return newStr;
  };
  String.prototype.trim = function () {
  var str = this;
  return str == '' ? str : str.replace(/(^\s*)/g, '').replace(/(\s*$)/g, '');
  };
  ///获得字符串实际长度,中文2,英文1
  ///要获得长度的字符串
  String.prototype.GetLength = function () {
  var realLength = 0, len = this.length, charCode = -1;
  for (var i = 0; i < len; i++) {
  charCode = this.charCodeAt(i);
  if (charCode >= 0 && charCode <= 128) realLength += 1;
  else realLength += 2;
  }
  return realLength;
  };
  String.prototype.ParseFloatAndToFixed = function (i) {
  return parseFloat(parseFloat(this).toFixed(i));
  };
  String.prototype.isNullOrWhiteSpace = function () {
  // null、 ''、' '、undefinded →→return true
  return this == '' || this.trim() == '';
  };
  String.prototype.startsWith = function (start, ignoreCase) { //start:欲判断字符, ignoreCase:是否忽略大小写
  var s = this;
  if (s.isNullOrWhiteSpace()) {
  return false;
  }
  if (ignoreCase) {
  s = s.toLowerCase();
  start = start.toLowerCase();
  }
  if (s.substr(0, start.length) == start) {
  return true;
  }
  return false;
  };
  String.prototype.endsWith = function (end, ignoreCase) { //end:欲判断字符, ignoreCase:是否忽略大小写
  var s = this;
  if (s.isNullOrWhiteSpace()) {
  return false;
  }
  if (ignoreCase) {
  s = s.toLowerCase();
  end = end.toLowerCase();
  }
  if (s.substr(s.length - end.length) == end) {
  return true;
  }
  return false;
  };
  String.prototype.replaceAll = function (s1, s2) {
  return this.replace(new RegExp(s1, "gm"), s2);
  };
  //数据中是否包括指定对象
  Array.prototype.contain = function (fun) {
  for (var item in this) {
  if (fun.constructor == Function) {
  if (fun(item) == true) return true;
  }
  }
  return false;
  };
  Array.prototype.get = function (fun) {
  for (var i in this) {
  if (fun.constructor == Function) {
  if (fun(this) == true) return this;
  }
  }
  return null;
  };
  Array.prototype.del = function (n) { //n表示第几项,从0开始算起。
  //prototype为对象原型,注意这里为对象增加自定义方法的方法。
  if (n > this.length - 1) {
  return false;
  } else {
  return n < 0 ? this : this.slice(0, n).concat(this.slice(n + 1, this.length));
  }
  /*
  concat方法:返回一个新数组,这个新数组是由两个或更多数组组合而成的。
  这里就是返回this.slice(0,n)/this.slice(n+1,this.length)
  组成的新数组,这中间,刚好少了第n项。
  slice方法: 返回一个数组的一段,两个参数,分别指定开始和结束的位置。
  */
  };
  Array.prototype.removeByValue = function (val) {
  if (!val) {
  return this;
  }
  for (var i = 0; i < this.length; i++) {
  if (this == val) {
  return this.del(i);
  }
  }
  };
  Array.prototype.indexOf = function (item, strict) { //strict:是否严格相等(===)
  var index = -1;
  strict = strict == 'undefined' ? true : strict;
  var length = this.length;
  if (strict) {
  for (var i = 0; i < length; i++) {
  if (this === item) {
  index = i;
  break;
  }
  }
  } else {
  for (var i = 0; i < length; i++) {
  if (this == item) {
  index = i;
  break;
  }
  }
  }
  return index;
  };
  View Code


最新回复 (23)
全部楼主
返回
发新帖
我也是有底线哒~