JS面向对象

经常碰到页面活动倒计时的效果,于是写了个js倒计时的方法,抽出来,直接引用Common.js文件就可以了。

Common.js文件:

var CountDown = {
    "now": new Date(),
    "LeftTime": function (t) { //t参数格式 : 2015/2/11 10:08:51
        var dt = new Date(t);
        var now = this.now;
        var disTime = dt - now;
        if (disTime <= 0) {
            return;
        }
        var days = parseInt(disTime / (1000 * 3600 * 24));
        var leftDay = parseInt(disTime % (1000 * 3600 * 24));
        var hours = parseInt(leftDay / (1000 * 3600));
        var leftHour = parseInt(leftDay % (1000 * 3600));
        var minutes = parseInt(leftHour / (1000 * 60));
        var leftMinute = parseInt(leftHour % (1000 * 60));
        var seconds = parseInt(leftMinute / (1000));
        var showDay = (days < 10) ? (0 + days.toString()) : days.toString();
        var showHour = (hours < 10) ? (0 + hours.toString()) : hours.toString();
        var showMinute = (minutes < 10) ? (0 + minutes.toString()) : minutes.toString();
        var showSecond = (seconds < 10) ? (0 + seconds.toString()) : seconds.toString();
        return showDay + "|" + showHour + "|" + showMinute + "|" + showSecond;
    }
};

 

使用的页面:

<script src="@Url.Content("~/Common.js")" type="text/javascript"></script>
<h2>Index</h2>

<script type="text/javascript">
    var time = "2015/3/11 10:01:15";
    var value = CountDown.LeftTime(time);
    var times = value.split(|);
    alert(times[0] + "" + times[1] + "" + times[2] + "" + times[3] +"");
</script>

 

 =============================================================================================

附加js面向对象知识:http://www.iteye.com/topic/434462

    //第1种写法  
    function Circle(r) {  
          this.r = r;  
    }  
    Circle.PI = 3.14159;  
    Circle.prototype.area = function() {  
      return Circle.PI * this.r * this.r;  
    }  
      
    var c = new Circle(1.0);     
    alert(c.area());   
    //第2种写法  
    var Circle = function() {  
       var obj = new Object();  
       obj.PI = 3.14159;  
         
       obj.area = function( r ) {  
           return this.PI * r * r;  
       }  
       return obj;  
    }  
      
    var c = new Circle();  
    alert( c.area( 1.0 ) );  
    //第3种写法  
    var Circle = new Object();  
    Circle.PI = 3.14159;  
    Circle.Area = function( r ) {  
           return this.PI * r * r;  
    }  
      
    alert( Circle.Area( 1.0 ) );  
    //第4种写法  
    var Circle={  
       "PI":3.14159,  
     "area":function(r){  
              return this.PI * r * r;  
            }  
    };  
    alert( Circle.area(1.0) );  
    //第5种写法  
    var Circle = new Function("this.PI = 3.14159;this.area = function( r ) {return r*r*this.PI;}");  
      
    alert( (new Circle()).area(1.0) );  

特别是最后两种,经常见到。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。