码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript通过原型给数组添加更多的方法

时间:2015-04-07 19:22:52      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

一、数组获取最大值的方法

     var arr = ["nba","haha","cba","aaa","sbc"];
        Array.prototype.getMax = function(){
    
          var temp = 0;
          for(var x=1; x<this.length; x++){
              if(this[x]>this[temp]){
                  temp = x;
              }
          }
          return this[temp];
      }
    alert(arr.getMax());//以上的this都表示arr这个数组,那个对象调用这个方法this就表示谁

 

 

 

二、数组的字符串表现形式,定义toString方法。 相当于java中的复写。

Array.prototype.toString = function(){
    return "["+this.join("/ ")+"]";
}
var arr=[32,23,32,34322,3232,2453];
alert(arr.toString());

 

JavaScript通过原型给数组添加更多的方法

标签:

原文地址:http://www.cnblogs.com/LO-ME/p/4398802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!