您的位置:首页 > Web前端 > JavaScript

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐