1. 程式人生 > >開發自己的類庫

開發自己的類庫

對象 ret sha his sda .get ets str 決定

上文說過,復用性主要有:插件,插件為功能的基本單元;組件,組件為應用的單元;設計模式,設計模式為解決為題的思路。

上述三者是算法的具體表現形式。

基於上面的認識,減少重復造輪子的時間,實現高效開發,決定開發自己的類庫。

下面是第一個函數,時間戳轉為具體時間;

//時間戳轉為具體時間
function transform(now) {
    var d = new Date(now);
    var year = d.getFullYear();
    var month = d.getMonth() + 1;
    var day = d.getDate();
    var hour = d.getHours();
    var minute = d.getMinutes();
    var second = d.getSeconds();
    if (month < 10) {
        month = "0" + month;
    }
    var date = year + "-" + month + "-" + day +
        " " + hour + ":" + minute + ":" + second;
    return date;
}

反過來具體時間轉為時間戳:

//具體時間轉轉為指定時間戳
function transdate(date) {
    var d = new Date();
    d.setFullYear(date.substring(0, 4));
    d.setMonth(parseInt(date.substring(5, 7)) - 1);
    d.setDate(date.substring(8, 10));
    d.setHours(date.substring(11, 13));
    d.setMinutes(date.substring(14, 16));
    d.setSeconds(date.substring(17, 19));
    return Date.parse(d) / 1000;
}

也可以寫成棉城對象的形式:

var transform = function() {

    };
    transform.prototype.change = function(t) {

        var d = new Date(t);
        this.year = d.getFullYear();
        this.month = d.getMonth() + 1;
        this.day = d.getDate();
        this.hour = d.getHours();
        this.minute = d.getMinutes();
        this.second = d.getSeconds();
        if (this.month < 10) {
            this.month = "0" + this.month;
        }
        this.date = this.year + "-" + this.month + "-" + this.day +
            " " + this.hour + ":" + this.minute + ":" + this.second;
        return this.date;


    }

好了,關於轉換的主函數就是這個了,實際應用當中具體時間轉為時間戳,會根據具體情況進行變化處理。

開發自己的類庫