201506101252_《JavaScript时间戳、转义、类型转化、继承》

例如要得到:20150610125348,可以这么做:

  1. function CurentTime()
    {
    var now = new Date();

    var year = now.getFullYear();         //年
    var month = now.getMonth() + 1;  //月
    var day = now.getDate();              //日

    var hh = now.getHours();              //时
    var mm = now.getMinutes();         //分
    var ss = now.getSeconds();           //秒

    var clock = year + "-";

    if(month < 10)
    clock += "0";

    clock += month + "-";

    if(day < 10)
    clock += "0";

    clock += day + " ";

    if(hh < 10)
    clock += "0";

    clock += hh + ":";
    if (mm < 10) clock += '0';
    clock += mm + ":";

    if (ss < 10) clock += '0';
    clock += ss;
    return(clock);
    };

alert(+new Date(CurentTime()));  

 

二. 转义

var url = encodeURIComponent('http://segmentfault.com/questions/newest');  //http://%&54....

decodeURIComponent(url);  //http://segmentfault.com/questions/newest

 

三. 小数点后几位

var someNum.toFixed(2);   //保留小数点后两位

 

四. 检测类型

var toJudge = Object.prototype.toString;

var whatType = toJudge.call(new Date);

alert(whatType);  //object,date

 

五. 继承

function  Shape() {

  this.x = 0;

      this.y = 0;

}

Shape.prototype.move = function(x,y) {

  this.x += x;

      this.y += y;

      console.info("This is moving action..."); 

}

 

function Rectangle() {

  Shape.call(this);

}

 

var rect = new Rectangle();

alert(rect instanceof Rectangle); //true

alert(rect instanceof Shape);  //true

rect.move(2,3);

 

posted @ 2015-06-10 13:17  Coca-code  阅读(201)  评论(0编辑  收藏  举报