JavaScript prototype, 扩充String对象
来源:互联网 发布:echarts读取json文件 编辑:程序博客网 时间:2024/06/04 20:00
Js功能强大灵活,不仅可以用来做网页客户端,还可以写asp后台代码,在Windows中与WSH的环境中更是强大。
以下代码只是我自编自用的Js类framework的一部分,经常被我用来做自动化测试,网站前后台。
扩充String对象的功能。
为String增加三个成员函数
- trim
- remove
- format --- 类似C函数的sprintf
和一个静态函数
- parseString
/* javascript prototype. * Authors: perry <ppsoft268@gmail.com> * Date : April 11, 2009 */ MY.ClassFactory.extend(String.prototype, { //public trim: function() { return this.replace(/^\s+|\s+$/g, ''); }, //public remove: function(s) { return this.replace(new RegExp(s, 'g'), ''); }, //public format: function() { var o = this, c = 0, z = ''; b = arguments; this.replace(/\%(\d*)(d|x|s)+?/gi, function(r0, r1, r2) { var n = parseInt(r1), x = '', k = r2, v = 0; if ( c < b.length ) { if ( n != n ) n = 0; for ( var i = 0; i < n; i++ ) x += '0'; switch ( k ) { case 'X': case 'x': case 'd': z = String.parseString(b[c]); v = parseInt(z.replace(/[-\+]+/g,'')); if ( v != v ) v = 0; x += v.toString(k == 'd' ? 10 : 16); if ( n > 0 ) x = x.substr(x.length - n); if ( k == 'X' ) x = x.toUpperCase(); if (/-\d+/g.test(z)) x = '-' + x; break; case 's': x = b[c]; break; default: x = ''; } } c++; o = o.replace(new RegExp(r0, 'i'), x); }); return o; } }, false); MY.ClassFactory.extend(String, { //public parseString: function(s) { if ( typeof s == 'boolean' ) return s.toString(); if ( typeof s == 'number' ) return s.toString(); if (s) return (new String(s)).valueOf(); return ''; } }, false);
- JavaScript prototype, 扩充String对象
- JavaScript prototype, 扩充Number对象
- JavaScript prototype, 扩充Date对象原
- 通过原型(prototype)扩充javascript内置对象
- JavaScript--String对象原型属性prototype
- 通过 prototype 为 JavaScript 的 String 对象添加方法(函数)
- Javascript(二)-05-(常见对象-String-原型属性prototype)
- JavaScript---对象学习(一)object对象、string对象、javaScript的constructor属性和prototype属性
- JavaScript的Prototype对象
- JavaScript [面向对象] prototype
- Javascript对象,prototype链
- JavaScript Prototype对象浅谈
- javaScript的prototype对象
- JavaScript原型对象prototype
- JavaScript/Jscript核心语言对象扩充函数
- JavaScript中String.prototype用法
- 利用prototype属性,对JavaScript对象进行功能扩展,比如数组对象Array,String等
- javascript [面向对象] prototype 分析
- 查看PL/SQL编译时的错误信息
- Java设计模式圣经连载(05)-代理模式
- nodejs测试: buffer和字符串相加对比测试
- 基于多核平台的实时动态体积云模拟
- Hardware Buffers
- JavaScript prototype, 扩充String对象
- HTTP 500 - 内部服务器错误 之三步解决方案
- —— Activity间数据传输 (六)
- 你或许该这样生活
- 主流报表制作工具之王者争夺战:功能大PK系列之动态参数
- c/c++: 多线程编程基础讲解(四)
- Java与模式:适配器模式
- 编写Shell代码规范
- 较长的数字字符串转换为整型