Array-练习-自定义功能

//html part

<script type="text/javascript" src="out.js"></script>
<script type="text/javascript">
/*
*给数组对象添加新功能,使用到原型属性
*/
var array = ["nba","haha","cba","aaa","abc"];

var maxValue = array.getMax();

document.write(maxValue+"<br/>");
document.write(array.toString());

</script>

 

//js part

/*
*数组获取最大值的方法
*/
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];

}

/*
*数组的字符串表现形式,定义toString()方法,相当于Java中的复写。
*/
Array.prototype.toString = function()
{
return "["+this.join(", ")+"]";
}

 

posted @ 2014-06-05 21:38  是但哥  阅读(164)  评论(0编辑  收藏  举报