1.Object对象
创建Object对象:
obj=new Object([value])
Object 对象的属性:
·prototype属性:该属性返回对象类型原型的调用。
可以使用该属性为对象添加方法,如:为Array对象添加一个求最大值的方法,如下所示
function array_max(){ var i=0,max=this[0]; for(i=1;i<this.length;i++){ if(max<this[i])max=this[i]; } return max; } function test(){ Array.prototype.max=array_max;//为Array对象添加求最大值的方法max() var obj=new Array(1,2,3,4,5,6); document.write("max:"+obj.max());//调用方法 }
在html文件中的调用如下:
<head> <script type="text/javascript" src="test.js"></script> </head> <body> <p><input type="button" onClick="test()" value="button"></p> </body>
·constructor属性:该属性表示创建对象的函数。如下:
//对象 var x=new String("hello"); if(x==String){ document.write(x); } //函数 function test(){ } var y=new test; if(y.constructor==test){ document.write("hello"); }
Object对象的方法:
·toLocaleString()方法:返回一个日期;对象必须是Date对象;
var date=new Date(); document.write(date.toLocaleString());
·toString()方法
·valueOf()方法:获取对象的值