日期:2014-05-16 浏览次数:20496 次
<script type="text/javascript"> <!-- //遍历数组 var dayArray=[1,2,3]; for(var day in dayArray){ document.write(dayArray[day]); } //定义对象,设置属性,访问属性 function University(){ } var university=new University(); university.name="云南省" university.city="昆明市" university.date="1999" university.sayHi=function(){document.write("hi,boy")} document.write("<br/>"); document.write(university) document.write(university.name) document.write(university.city) document.write(university['date']) document.write(university.sayHi()) document.write("<br/>"); for (var prop in university){ document.write(university[prop]);//形如university['date'] } //eval document.write("<br/>"); document.write("计算字符串表达式的值:"+eval("8+9+5/2")); var obj=new Object; obj.color='red'; obj.dodo=function(){alert(34)}; document.write(obj.color); //对比 document.write(obj.dodo()); document.write(obj.dodo); //工厂方式创建对象 function fnshowPinpai() { alert(this.color); } function createcar(sColor,iDoors,sPinpai) { var oTempCar=new Object; oTempCar.color=sColor; oTempCar.doors=iDoors; oTempCar.pinpai=sPinpai; oTempCar.showPinpai=fnshowPinpai; return oTempCar; } var oBmw=createcar("black",4,"BMW"); oBmw.showPinpai(); //构造函数方式创建对象 function Car(scolor,snum) { this.color=scolor; this.dodo=function(){alert('aa')}; } var obj=new Car('yellow',30); for(prop in obj) { alert(prop);//color;dodo } //原型方式创建对象 function Car(scolor,snum) { this.color=scolor; this.dodo=function(){alert('aa')}; } Car.prototype.getColor = function (){ return this.color; } Car.prototype.setColor = function (value){ this.color = value; } var obj=new Car('yellow',30); obj.setColor(666); alert(obj.getColor());//666 for(prop in obj) { alert(prop);//color;dodo } //--> </script>