JavaScript对象-自定义对象、Math对象、Array对象、Date对象、String对象

一:javascript对象
<body>
<p>
    自定义对象
</p>
<script>
    document.writeln("第一种方式<br/>");
    people=new Object();
    people.name="ymt";
    people.age=26;

    document.write("name:"+people.name+"<br/>age:"+people.age);

    document.writeln("<br/>第二种方式<br/>");
    people={name:"ymt",age:26};
    document.write("name:"+people.name+"<br/>age:"+people.age);

</script>

<p>
    使用函数自定义对象
</p>
<script>
    function people(name,age){
        this.name=name;
        this.age=age;
    }
    son=new people("ymt",26);
    document.write("name:"+son.name+"<br/>age:"+son.age);
</script>
</body>

二:String对象
<body>
<p>
    <h1>注意下面两种中test的不同</h1>
</p>
<p>
    第一种方式
</p>
<script>
    var del,vel;
    del="这是直接使用字符串显示创建";
    vel="这是直接使用字符串显示创建";
    del.test="12";

    document.write("del.test:"+del.test+"<br/>vel.test:"+vel.test);
</script>
<p>
    第二种方式
</p>
<script>
    var gamma, delta;
    gamma = new String("这是一个字符串");
    delta = new String("这是也一个字符串");
    gamma.test = 10;
    document.write("gamma.test:"+gamma.test+"<br/>delta.test:"+delta.test);
</script>
<p>
<h1>constructor属性</h1>
</p>
<script>
    hi=new String("hi");
    if(hi.constructor==String){
        document.write("这是一个String类型<br/>");
    }

    function MyFunc(){

    }
    var y=new MyFunc;
    if(y.constructor==MyFunc){
        document.write("这是一个MyFunc")
    }
</script>
<p>
<h1>prototype属性</h1>
</p>
<script>
    function array_max(){
        var i,max=this[0];
        for(i=0;i<this.length;i++){
            if(max<this[i]){
                max=this[i];
            }
        }
        return max;
    }
    Array.prototype.max=array_max;
    x=new Array(1,2,3,4,5,6);
    var y= x.max();

    document.write("the max of x is:"+y);
</script>
</body>

三:Date对象
<body onload="getTime()">
<script>
    function getTime(){
        var date=new Date();
        var riqi="today is :";
        riqi=date.getFullYear()+"-";
        riqi+= date.getMonth()+1+"-";
        riqi+= date.getDay();

        var h=date.getHours();
        var m=date.getMinutes();
        m=checkTime(m);
        var s=date.getSeconds();
        s=checkTime(s);
        document.getElementById("time").innerHTML=riqi+" "+h+":"+m+":"+s;
        setTimeout(function(){
            getTime();
        },1000);
    }
    function checkTime(i){
        if(i<10){
            i="0"+i;
        }
        return i;
    }
</script>
<div id="time"></div>
<script>
    d = new Date(Date.UTC(1996, 4, 8, 16, 30));
    document.write(d);
</script>
</body>

四:Array对象
<body>
<p>
    <h1>介绍数组对象</h1>
</p>
<script>
    a=new Array("5","3","2","1","4");

    document.write(a+"<br/>");
    document.write(a.sort(function(a,b){
        return a-b;
    })+"<br/>");
    document.write(a.sort(function(a,b){
        return b-a;
    }));
</script>
<p>
<h1>数组的常用的方法</h1>
</p>
<ui>
    <li>Array.<big>concat</big></li>
    <li>Array.<big>reserve</big></li>
    <li>Array.<big>shift</big></li>
    <li>Array.<big>push</big></li>
    <li>Array.<big>jion</big></li>
</ui>
</body>

五:Math对象
<body>
<p>
    Math的一些常用的方法
</p>
<script>
    document.write("Math.abs(-10):");
    document.write(Math.abs(-10));
    document.write("<br/>");

    document.write("parseInt(Math.random()*10):");
    document.write(parseInt(Math.random()*10));
    document.write("<br/>");

    document.write("Math.round(20.4):");
    document.write(Math.round(20.4));
    document.write("<br/>");
</script>
</body>

posted @ 2015-10-09 13:56  杨MT  阅读(159)  评论(0编辑  收藏  举报