前端开发学习Day33

260 阅读1分钟

第33天,把函数剩下的知识学完之后开始学习对象了。我觉得对象比函数还要复杂,可能是我现在已经产生了畏难心理,不自信,也静不下心了吧!有个输出表格的例子,我在学for循环的时候写了一遍,在学JavaScript的时候写了一遍,今天学习对象的时候,是第三次遇到,我发现自己还是不会写。我凌乱了……

  <script>
function Table(row,col,width,height){
	this.row = row;			//对象的row属性
	this.col = col;				//对象的col属性
	this.width = width;			//对象的width属性
	this.height = height;			//对象的height属性
	Table.prototype.create=function(){
		var tableStr="";
		tableStr+=("<table width="+this.width+" height="+this.height+" align=center border=1>");
		var bgcolor;										//声明背景颜色的变量
		for(var i=0; i<this.row; i++){										//指定外层循环
			if(i%2 == 0){									//设置奇数行和偶数行的背景颜色
				bgcolor = "#FFFFFF";
			}else{
				bgcolor = "#EEEEEE";
			}
			tableStr+=("<tr bgcolor="+bgcolor+">");
			for(var j=0; j<this.col; j++){									//指定内层循环
				tableStr+=("<td width=50 align=center></td>");
			}
			tableStr+=("</tr>");
		}
		tableStr+=("</table>");
		document.write(tableStr);
	}
}
var Table1 = new Table(3,6,500,150);		//创建对象Table1
Table1.create();

    </script>