zoukankan      html  css  js  c++  java
  • 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>
    


  • 相关阅读:
    android 网络 post get
    java 命名规范
    android 判断service是否开启
    android 无线连接eclipse
    eclipse jar java.lang.NoClassDefFoundError
    eclipse 默认 utf8
    timer timetask handler
    android 自定义动画按钮
    设计网站
    java 分解arraylist中单个对象 的属性名与值
  • 原文地址:https://www.cnblogs.com/wanghang/p/6299777.html
Copyright © 2011-2022 走看看