javascript-prototype

it2025-03-13  23

在javascript中所有的类的祖先类都是Object,实例对象的prototype属性是对父类的应用,而object的prototype为null,因为他没有父类。子类继承父类的所有属性和方法,通过prototype给父类增加一些方法,在所有的子类对象都可以使用这些方法。比如:可以给Array添加一个方法,返回数组里最大的数

 1 function  array_max()  2 { 3   var max=this[0]; 4   for(var i=0;i<this.length;i++) 5{ 6max=max>this[i]?max:this[i]; 7} 8return max; 9} 10 Array.prototype.max = array_max; 11 Array arr = [ 1 , 2 , 7 , 5 , 8 , 3 ]; 12 var  mm  =  arr.max(); // mm gets 8 查看示例结果

转载于:https://www.cnblogs.com/HappyQQ/archive/2008/02/26/1082951.html

最新回复(0)