JavaScript prototype 属性 扩展你的方法
2011-04-14 20:31
218 查看
JavaScript prototype 属性
prototype 属性使您有能力向对象添加属性和方法。(1)Number.prototype.addNum
作用:数字相加
实现:Number.prototype.addNum = function (num) { return (this + num); };
实例:alert((3).addNum(5));
(2) String.prototype.ToCNString
作用:转换为中文
实现:String.prototype.ToCNString = function () { return "这是中文的字符"; };
实例:alert(("").ToCNString());
(3)String.prototype.CNLeng
作用:有双字节字符串的长度
实现: String.prototype.CNLeng = function () { var arr = this.match(/[^/x00-/xff]/ig); return this.length + (arr == null ? 0 : arr.length) };
实例:alert(("长度").CNLeng());
代码:
<mce:script type="text/javascript"><!-- String.prototype.ToCNString = function () { return "这是中文的字符"; }; String.prototype.CNLeng = function () { var arr = this.match(/[^/x00-/xff]/ig); return this.length + (arr == null ? 0 : arr.length) }; Number.prototype.addNum = function (num) { return (this + num); }; function NaNTest() { alert((3).addNum(5)); alert(("长度").CNLeng()); alert(("").ToCNString()); } // --></mce:script>
相关文章推荐
- 自己写PHP扩展之操作类的属性和方法
- 一个用户实体应该有它自己的实体验证,即一个实体应该有它的属性,方法,扩展属性及验证机制组成
- Kotlin的扩展属性和扩展方法
- 标签自定义属性,获取和操作的方法封装以及在此基础上对标签原有属性的扩展...
- C#可扩展编程之MEF学习笔记(三):导出类的方法和属性
- iOS 扩展类添加属性,需要实现的set方法
- WIN8系统安装软件时提示"扩展属性不一致"的解决方法
- C#3.0 之 自动属性 对象初始化器,集合初始器,扩展方法 (转)
- 转载:JavasSript声明类、方法、属性、扩展方法、静态方法、继承
- 《ASP.NET MVC验证框架中关于属性标记的通用扩展方法》之继续扩展
- C#学习笔记四: C#3.0自动属性&匿名属性及扩展方法
- [C#详解] (1) 自动属性、初始化器、扩展方法
- [置顶] 预设置只读文件系统squashfs上的文件的扩展属性的方法
- FME中Cass扩展属性转Shp的方法
- 步步为营VS 2008 + .NET 3.5(3) - C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Extension Methods(扩展方法)
- 《从零开始学Swift》学习笔记(Day 50)——扩展计算属性、方法
- C#3.0 之 自动属性 对象初始化器,集合初始器,扩展方法
- C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Extension Methods(扩展方法)
- Kotlin(2): 优雅地扩展类的方法和属性
- C#3.0 之 自动属性 对象初始化器,集合初始器,扩展方法 (转)