2014-10-26 59 views
-2

我想通過使用getTime()功能將對象屬性添加到JS中的Date對象。Javascript通過原型將對象添加到日期

Date.prototype.stdDate={ 
     Hour:Math.ceil(getTime()/(1000 * 3600)), 
     Day:Math.ceil(getTime()/(1000 * 3600 * 24)), 
     Week:Math.ceil(getTime()/(1000*3600*24*7)), 
     Year:Math.ceil(getTime()/(1000*3600*24*365.242)) 
    } 

,但我一直在收到此錯誤,而我的鉻控制檯上測試:

Uncaught ReferenceError: getTime is not defined 

我理解的原因是,在功能範圍無法訪問的getTime(),它裏面的日期,當在全球和當地的背景下找到它而沒有發現它時,它最終會拋出這個錯誤。我也嘗試過thisthis.getTime()

還是:Uncaught TypeError: undefined is not a function

你們仍然可以嘗試在控制檯上

所以我的問題是如何做到這一點的方法不對人?

+0

在日期對象上使用getTime。 var d = new Date; d.getTime() – Fyre 2014-10-26 14:52:07

回答

2

您的代碼正在嘗試致電getDate作爲您將其添加到專業版的那一刻輸入。相反,你想稍後調用它,當這個屬性被訪問時。

您有兩種選擇:方法或帶有「getters」的屬性。在你的情況,我想只有一個方法:

Date.prototype.stdDate = function(part) { 
    switch (String(part).toLowerCase()) { 
     case "hour": 
      return Math.ceil(this.getTime()/(1000 * 3600)); 
     case "day": 
      return Math.ceil(this.getTime()/(1000 * 3600 * 24)); 
     case "week": 
      return Math.ceil(this.getTime()/(1000*3600*24*7)); 
     case "year": 
      return Math.ceil(getTime()/(1000*3600*24*365.242)); 
     default: 
      // What you think appropriate 
    } 
}; 

注意,這裏我定義只是一個功能,stdDate,它接受一個字符串參數:

var hour = dt.stdDate("hour"); 

這是因爲如果我們將stdDate引用到對象上並將方法放在對象上,則this不會再引用該日期。 (有辦法解決這個問題,但它們比這裏可能需要的更麻煩)。

在ES5驅動的引擎上,製作dt.stdDate.Hour和這樣的工作是可能的(但可能更麻煩)

// On the prototype, define a property called `stdDate` that has a getter 
Object.defineProperty(Date.prototype, "stdDate", { 
    get: function() { 
     // This is the getter: Create an object with properties for 
     // Hour, Day, and such which have getters. The getters are 
     // "bound" to `this` using `Function#bind`: 
     var stdDate = {}; 
     Object.defineProperties(stdDate, { 
      Hour: { 
       read: function() { 
        return Math.ceil(this.getTime()/(1000 * 3600)); 
       }.bind(this), 
      }, 
      Day: { 
       read: function() { 
        return Math.ceil(this.getTime()/(1000 * 3600 * 24)); 
       }.bind(this), 
      }, 
      Week: { 
       read: function() { 
        return Math.ceil(this.getTime()/(1000*3600*24*7)); 
       }.bind(this), 
      }, 
      Year: { 
       read: function() { 
        return Math.ceil(this.getTime()/(1000*3600*24*365.242)); 
       }.bind(this) 
      } 
     }); 

     // Now, redefine the `stdDate` property on this specific instance 
     Object.defineProperty(this, "stdDate", { 
      value: stdDate 
     }); 

     // And return it 
     return stdDate; 
    } 
}); 

(你可能要與鼓搗得到它完美的工作,我衝出來的,不能馬上進行調試,但這個想法是存在的。)

那是真的很費解並且可能不是一個好主意,但是如果API dt.stdDate.Hour對您來說真的很重要,那麼可能是

+0

感謝您的回答!我想知道如果我做Date.stdDate = function()而不是Date.prototype.stdDate = function()它會改變什麼嗎? – user3135757 2014-10-26 15:06:15

+0

@ user3135757:是的,這是完全不同的。而不是將屬性添加到所有'Date'實例的原型中,而是將其添加到'Date'函數;實例無法訪問它(除了通過「Date.stdDate」)。 – 2014-10-26 15:18:50

3

您需要使用this.getTime()調用對象的方法.getTime()它運行於:

Date.prototype.stdDate={ 
     Hour:Math.ceil(this.getTime()/(1000 * 3600)), 
     Day:Math.ceil(this.getTime()/(1000 * 3600 * 24)), 
     Week:Math.ceil(this.getTime()/(1000*3600*24*7)), 
     Year:Math.ceil(this.getTime()/(1000*3600*24*365.242)) 
    } 

編輯:

哦,你需要將它定義爲一個函數...

Date.prototype.stdDate= function() {return{ 
     Hour:Math.ceil(this.getTime()/(1000 * 3600)), 
     Day:Math.ceil(this.getTime()/(1000 * 3600 * 24)), 
     Week:Math.ceil(this.getTime()/(1000*3600*24*7)), 
     Year:Math.ceil(this.getTime()/(1000*3600*24*365.242)) 
    }} 
+0

我已經試過了:Uncaught TypeError:undefined不是函數消息:「undefined不是函數」 – user3135757 2014-10-26 14:53:38

+0

已修復。你需要將它定義爲一個函數。 – Scimonster 2014-10-26 14:56:06

+0

謝謝我想知道它會改變一些情況,如果你使用Date.stdDate而不是Date.prototype.stdDate – user3135757 2014-10-26 15:11:04

相關問題