JS对象prototype 属性

来源:互联网 发布:calendar.js api 编辑:程序博客网 时间:2024/05/01 07:05
prototype 属性
返回对象类型原型的引用。
objectName.prototype
objectName 参数是对象的名称。 
说明
用 prototype 属性提供对象的类的一组基本功能。 对象的新实例“继承”赋予该对象原型的操作。 
例如,要为 Array 对象添加返回数组中最大元素值的方法。 要完成这一点,声明该函数,将它加入 Array.prototype, 并使用它。 
function array_max(){   var i, max = this[0];   for (i = 1; i < this.length; i++)   {   if (max < this[i])   max = this[i];   }   return max;}Array.prototype.max = array_max;var x = new Array(1, 2, 3, 4, 5, 6);var y = x.max();
该代码执行后,y 保存数组 x 中的最大值,或说 6。