在javascript中我们通过prototype属性可以来为一个已存在的类创建新方法。 如Number类是javascript提供的一个原始类,我们现在给它添加一个toHexString方法,这个方法用来返回整数的16进制结果 Number.prototype.toHexString = function () { return this.toString(16);} var i = 15 ;alert(i.toHexString()); // 显示结果为f 通过这个方式我们可以对已存在的类增加我们需要的新方法。 通过prototype属性,也可以对已经存在的类进行重定义。 如原本Function的toString方法是返回函数的原代码,我们可以重定义输出其它的文字 Function.prototype.toString = function () { alert("this is a function");} function hello() { alert("hello");} alert(hello.toString()); // 显示为this is a function 转载于:https://www.cnblogs.com/zjb3165/archive/2008/06/26/1230441.html