小说网站 搜小说 无限网 烟雨红尘 小说爱好者 免费小说 免费小说网站

javascript之prototype原型属性

这个地方有点绕,仔细理解代码的意义。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript"> 
/*
需求:想把getMax与searchEle方法添加 到数组对象中。
 
	functoin Array(){
		this.prototype = new Object();	
		
		this.getMax = function(){
		
		}
	}	
Prototype注意的细节:
	1.	prototype是函数(function)的一个必备属性(书面一点的说法是"保留属性")(只要是function,就一定有一个prototype属性)
	2.	prototype的值是一个对象
	3.	可以任意修改函数的prototype属性的值。
	4.	一个对象会自动拥有prototype的所有成员属性和方法。
	
 
 
	
*/
	
	Array.prototype.getMax = function(){//给<span style="font-family: Arial, Helvetica, sans-serif;">prototype添加属性,就相当于给</span><span style="font-family: Arial, Helvetica, sans-serif;">Array对象添加同样的属性,即把这个功能放在了Array对象里面了。</span><span style="font-family: Arial, Helvetica, sans-serif;">
</span>

		var max = this[0];
		for(var index = 1; index<this.length ; index++){
			if(this[index]>max){
				max = this[index];	
			}	
		}
		return max;
	}
	
	
	Array.prototype.searchEle = function(target){
		for(var i = 0 ; i<this.length ; i++){
			if(target==this[i]){
				return i;	
			}	
		}
		return -1;
			
	}
	
 
 
	//var arr = new Array(12,4,17,9);
	var arr = [12,4,17,9];
	var max = arr.getMax();
	var index = arr.searchEle(9);
	document.write("最大值:"+ max+"<br/>");
	document.write("索引值:"+ index+"<br/>");
 
 
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
 
<body>
</body>
</html>


posted on 2016-05-23 22:53  王小航  阅读(165)  评论(0编辑  收藏  举报

导航