Share some commonly used extensions of js objects

不言
Release: 2018-07-11 17:11:23
Original
1100 people have browsed it

这篇文章主要介绍了关于分享一下js对象常用的扩展,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

//js string对象扩展 (function() { // 除去两边空白 String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ""); }; //截取字符串了 String.prototype.cutStr = function(len) { var str = this; var str_length = 0; var str_len = 0; str_cut = new String(); str_len = str.length; for (var i = 0; i < str_len; i++) { a = str.charAt(i); str_length++; if (escape(a).length > 4) { //中文字符的长度经编码之后大于4 str_length++; } str_cut = str_cut.concat(a); if (str_length >= len) { str_cut = str_cut.concat("..."); return str_cut; } } //如果给定字符串小于指定长度,则返回源字符串; if (str_length < len) { return str; } }; // 保留中文 String.prototype.getCn = function() { return this.replace(/[u4e00-u9fa5uf900-ufa2d]/g, ""); }; // 保留数字 String.prototype.getNum = function() { return this.replace(/[^\d]/g, ""); }; // 保留字母 String.prototype.getEn = function() { return this.replace(/[^A-Za-z]/g, ""); }; //从右边截取,到终点字符位置 String.prototype.subIndexStr = function(str) { var index = this.indexOf(str); return this.substr(0, index); }; // 从左截取指定长度的字串 String.prototype.left = function(n) { return this.slice(0, n); }; // 从右截取指定长度的字串 String.prototype.right = function(n) { return this.slice(this.length - n); }; // HTML编码 String.prototype.HTMLEncode = function() { var re = this; var q1 = [/x26/g, /x3C/g, /x3E/g, /x20/g]; var q2 = ["&", "<", ">", " "]; for (var i = 0; i < q1.length; i++) re = re.replace(q1[i], q2[i]); return re; }; //字符串转换为日期格式 String.prototype.toDate = function() { if (isNaN(this) && !isNaN(Date.parse(this))) {   return new Date(this); } else { console.error(this == null ? "不是日期格式" : this + "不是日期格式") } }; //左边补零 String.prototype.padLeftZero = function(n) { return Array(n > this.length ? (n - ('' + this).length + 1) : 0).join(0) + this; }; //右边补零 String.prototype.padRightZero = function(n) { return this + Array(n > this.length ? (n - ('' + this).length + 1) : 0).join(0); }; //转换人民币格式 String.prototype.toRenminbi = function() { var n = this.replace(/,/g, '');; var fraction = ['角', '分']; var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']; var unit = [ ['元', '万', '亿'], ['', '拾', '佰', '仟'] ]; var head = n < 0 ? '欠' : ''; n = Math.abs(n); var s = ''; for (var i = 0; i < fraction.length; i++) { s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, ''); } s = s || '整'; n = Math.floor(n); for (var i = 0; i < unit[0].length && n > 0; i++) { var p = ''; for (var j = 0; j < unit[1].length && n > 0; j++) { p = digit[n % 10] + unit[1][j] + p; n = Math.floor(n / 10); } s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s; } return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整'); }; String.prototype.amountFormat = function() { var number = this; var decimals = 2; number = (number + '').replace(/[^0-9+-Ee.]/g, ''); var n = !isFinite(+number) ? 0 : +number, prec = !isFinite(+decimals) ? 0 : Math.abs(decimals), sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep, dec = (typeof dec_point === 'undefined') ? '.' : dec_point, s = '', toFixedFix = function(n, prec) { var k = Math.pow(10, prec); return '' + Math.floor(n * k) / k; }; s = (prec ? toFixedFix(n, prec) : '' + Math.floor(n)).split('.'); var re = /(-?\d+)(\d{3})/; while (re.test(s[0])) { s[0] = s[0].replace(re, "$1" + sep + "$2"); } if ((s[1] || '').length < prec) { s[1] = s[1] || ''; s[1] += new Array(prec - s[1].length + 1).join('0'); } return s.join(dec); }; })(); //js Date 时间对象扩展 (function() { Date.prototype.toString = function(fmt) { function padLeftZero(str) { return ('00' + str).substr(str.length); } if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length)); } let o = { 'M+': this.getMonth() + 1, 'd+': this.getDate(), 'h+': this.getHours(), 'm+': this.getMinutes(), 's+': this.getSeconds() }; for (let k in o) { if (new RegExp(`(${k})`).test(fmt)) { let str = o[k] + ''; fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str)); } } return fmt; } })();
Copy after login

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

分享Nodejs接入微信JS-SDK的全过程

对于JS模块化的解析

对于Javascript加载的解析

The above is the detailed content of Share some commonly used extensions of js objects. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!