引用类型之object和date详解

引用类型的值是引用类型的实例,js中的引用类型是一种数据类型,用于将数据和功能组织在一起(也可叫对象定义,因为描述一类对象具有的属性和方法)


1、Object类型

大多数引用类型都是object类型,创建object类型实例可以new,也可以字面量表示(简化创建包含大量属性的对象)

       //创建对象
       var obj1=new Object();    //new操作符
       var obj2={}               //也是创建对象
       var obj3={                //字面量表示,分号和逗号
               name:"double",
               age:20,
               sex:"man"
       }

字面量方法也可以向函数传递大量可选参数,比如

       function displayInfo(argu){
                var output=""
                if(typeof argu.name=="string"){
                         output+="Name "+argu.name+"\n";
                }
                if(typeof argu.age=="number"){
                         output+="Age "+argu.age+"\n"
                }
                console.log(output)
       }
       displayInfo({      
              name:"double",
              age:20
       })
       displayInfo({
              name:"single"
       })
       //这种传参数模式适合需要向函数传入大量可选参数的情形

[]和点号大多时候可以互换

       var personName="name"           //[]可以通过变量访问属性,还有属性名非字母非数字情况
       alert(person[personName])

       person["first name"]="double"

2、Date类型

创建Date实例,在不传递参数情况下是自动获得当前的时间和日期的。创建特定的日期和时间则必须传入表示该日期的毫秒数,js提供两个方法Date.parse()和Date.UTC()

ES5添加新的方法Date.new()

    Date.prototype.myMet=function(){        //为Date添加一个原型属性
        if(this.getMonth()==0){
              this.myPrope="january"
        }
        if(this.getMonth()==1){
              this.myPrope="Febrary"
        }
        if(this.getMonth()==11){
              this.myPrope="December"
        }
    }
    var mydate=new Date()    
    mydate.myMet()                         //调用该方法
    var monthName=mydate.myPrope           //创建一个新对象
    console.log(monthName)
    

    var start=Date.now()
    console.log(start)    //返回调用该方法时的日期和时间的毫秒数
    
    console.log(now.toDateString())          //年月日
    console.log(now.toTimeString())          //时分秒
    console.log(now.toLocaleDateString())    //本地化年月日
    console.log(now.toLocaleTimeString())    //本地化时分秒
    console.log(now.toUTCString())           //完整的UTC日期

    //日期组件化的方法
    var date=new Date()
    console.log(date.getFullYear())      //获得年月日星期时分秒 毫秒
    console.log(date.getMonth())  0表示一月,11表示12月
    console.log(date.getDate())
    console.log(date.getDay())    0表示星期日,6表示星期六
    console.log(date.getHours())
    console.log(date.getMinutes())
    console.log(date.getSeconds())
    console.log(date.getMilliseconds())  
    console.log(date.getTime())          //整个日期的毫秒数
    
    //设置日期
    var newdate=new Date()
    newdate.setFullYear(2019)
    newdate.setMonth(0)    //0——11表示月,多出则增加年
    newdate.setDate(0)     //0表示上个月的最后一天;-1时上个月的倒数第二天
    newdate.setDate(newdate.getDate()+5)    //设置日期对象的后五天
    console.log(newdate)
    

关于日期的实例

 一个简单的时间同步

   window.onload=function(){
        change()
   }
    
   function checktime(i){      //判断时间的是否为个位数时
           if(i<10){
               i="0"+i
           }
           return i
   }

   function change(){
         var demo=document.getElementById("demo")
        var date=new Date()
        var year=date.getFullYear()
        var month=date.getMonth()+1
        var dates=date.getDate()
        var day=date.getDay()
        var hour=date.getHours()
        var minute=date.getMinutes()
        var second=date.getSeconds()

        hour=checktime(hour)
        minute=checktime(minute)
        second=checktime(second)

           var weekday=new Array(7)    //利用数组来卡星期
           weekday[0]="星期日"
           weekday[1]="星期一"
           weekday[2]="星期二"
           weekday[3]="星期三"
           weekday[4]="星期四"
           weekday[5]="星期五"
           weekday[6]="星期六"

           demo.innerHTML=year+"年"+month+"月"+dates+"日"+weekday[day]+hour+":"+minute+":"+second           
     }
     setInterval(change,1000)

 

posted @ 2018-01-30 15:22  决起而飞  阅读(1689)  评论(0编辑  收藏  举报