javascript声明对象

对象:

基于对象:将相关的操作使用一个对象完成,看做是一个整体

字符串对象
数学对象
数组对象
时间日期对象

对象存放的内容:
属性(变量)
方法

对象实例.属性(取值,赋值)
对象实例["属性名"]
对象实例.方法

获取时间:今天是几月几日几时几分星期几
<script>
var dt=new Date();
var str="今天是: ";
str+=dt.getYear()+"年";
str+=(dt.getMonth()+1)+"月";
str+=dt.getDate()+"日";
str+=dt.getHours()+":";
str+=dt.getMinutes()+":";
str+=dt.getSeconds()+" 星期";
switch(dt.getDay())
{
case 0:
    str+="日";
break;
case 1:
    str+="一";
break;
case 2:
    str+="二";
break;
case 3:
    str+="三";
break;
case 4:
    str+="四";
break;
case 5:
    str+="五";
break;
case 6:
    str+="六";
break;
}
document.write(str);
</script>


自定对象:
<script>
function play(){
var p=new Object();
p.width=200;
p.height=300;
p.num=4;
p.autotime=3;
p.autoplay=function()
{
alert("play...");
alert(this.num);
}
p.test=function()
{}
return p;
}
var p=play();
alert(p.width);
p.autoplay();
</script>


<script>
function play(width,height,num)
{
    this.width=width;
    this.height=height;
    this.num=num;
    this.autoplay=function()
    {
        alert("######");
    }
    this.test=function()
    {

    }
}
var p=new play(200,300,8);
var pro=" ";
for(pro in p)
{
    document.write("p."+pro+"="+p[pro]+"<br>");
}
</script>


对象遍历:
var pro="";
for (pro in p)
{
document.write("document."+"+pro+"+"="+"document[pro]+"<br>");
}

with(对象)
{
所有方法如果不加对象都是,括号对象的方法
}
document.write("<tabel>");
document.write("<tr>");
documnet.write("</tr>");
document.write("</table>");

with(document)
{
write("<tabele>");
write("<tr>");
write("</tr>");
write("</table>");

猜你喜欢

转载自blog.csdn.net/abenazhan/article/details/77116069
今日推荐