zoukankan      html  css  js  c++  java
  • JS中的对象数组

    <html>
    <head>
    	<title>对象数组的字符串表示</title>
    	<script type="text/javascript">
    		function Point(x,y){
    			this.x = x;
    			this.y = y;
    
    		}
    
    		function display(arr){
    			for (var i=0;i<arr.length;i++) {
    				alert(arr[i].x+","+arr[i].y);
    
    			}
    		}
    
    		//求X平均值
    		function averge(arr){
    			var totalx = 0;
    			var totaly = 0;
    			for (var i = 0;i<arr.length;i++) {
    
    				totalx+=arr[i].x;
    				totaly+=arr[i].y;
    
    
    			}
    
    			totalx = totalx/arr.length;
    			totaly = totaly/arr.length;
    
    			var pav = new Point(totalx,totaly);
    
    			return pav;
    
    		}
    
    		var p1 = new Point(1,2);
    		var p2 = new Point(3,4);
    		var p3 = new Point(5,6);
    
    		var points = [p1,p2,p3];
    
    		function A() {
    			//调用返回值是一个Point的对象
    
    			var number = averge(points);
    
    			alert(number.x+","+number.y);
    		}
    
    	
    	</script>
    </head>
    <body>
    <input type="text" name="name" id="name">
    <input type="button" value="dian" onclick="A();">
    
    </body>
    </html>
    

          在上述的代码中,创建对象的方法跟java是不是很像,只是javascript只是没有CLass的概念,但是function的作用特别强大,javascript创建对象的方法有很多中的方法,至少三种往上,等到后面的章节再详细的说。

    只是模拟一下对象数组的操作,大家可以写写。

  • 相关阅读:
    10月9日学习日志
    10月2日学习日志
    11月3日学习日志
    10月5日学习日志
    10月6日学习日志
    10月7日学习日志
    11月4日学习日志
    AccessDatabaseEngine.exe 32位64安装失败问题
    国产各安卓系统接收消息在杀进程后
    SAP容差组
  • 原文地址:https://www.cnblogs.com/airycode/p/4826826.html
Copyright © 2011-2022 走看看