簡體   English   中英

在Javascript OOP中調用嵌套函數

[英]Calling nested functions in Javascript OOP

我試圖變得更加熟悉Javascript OOP,所以我寫了一些測試腳本,但是在測試它時卻不斷出現異常:

例外:

Uncaught TypeError: Cannot call method 'day' of undefined

碼:

(function () {
    function Time (date) {
        var self = this;
        var timeInWeek = 604800000;
        var timeInDay = 86400000;
        var dateInMilliSeconds = date.getTime();

        self.add = function (num) {
            self.day = function () {
                var newDate = new Date();
                newDate.setTime(dateInMilliSeconds + (timeInDay * num));
                return newDate;
            };
        };
    };
    var date = new Date();
    var time = new Time(date).add(1).day();
    console.log(time);
})();

而且,當我在IIFE模式之外運行測試腳本時,出現了Time is undefined的異常,這是Java OOP的新手,因此,當我嘗試閱讀其他Javascript庫時,我感到頭大了。 任何幫助表示贊賞。

add方法返回undefined,因為它沒有return語句。 該問題與IIFE的使用無關。

取而代之的是, add方法只是在執行時添加了day方法(可能覆蓋了它),因此以下代碼將“起作用”:

var time = new Time(date)
t.add(1);    // returns undefined, but has a side-effect of adding "day" to t
t.day();     

但是,我懷疑這個問題有兩個方面:

  1. add應該return兼容類型對象方法鏈接 ; 這可以是同一對象(用於可變設計),也可以是新對象(用於不可變設計)。

  2. 應將day直接添加到每個Time對象; 這樣可以使new Time(date).day()起作用。

例如:

function Time (date) {
    var self = this;
    var timeInWeek = 604800000;
    var timeInDay = 86400000;
    var dateInMilliSeconds = date.getTime();

    self.add = function (num) {
        // The idea here is to return an object of the same type for "chaining".
        // Here I returned a new Time object (you'll have to work out the details),
        // although for mutable objects, "return self" would be appropriate.
        return new Time(dateInMilliSeconds + (timeInDay * num));
    };
    self.day = function () {
        // actually return the "day", whatever that is.
        return ...;
    };
};

盡管這可能是一個不錯的練習,但是對於生產代碼,我建議您使用moment.js,除非有其他令人信服的理由。 moment.js的源代碼(可能會提供很好的參考)在github / moment上 “添加”(省略和附加注釋)如下所示:

add : function (input, val) {
    // mutate this objects data (but not methods)
    addOrSubtractDurationFromMoment(this, dur, 1);
    // returns the same object for chaining
    return this;
},

您已經錯過了return this; (必須存在以允許菊花鏈連接)

function Time (date) {
        var self = this;
        var timeInWeek = 604800000;
        var timeInDay = 86400000;
        var dateInMilliSeconds = date.getTime();

        self.add = function (num) {
            self.day = function () {
                var newDate = new Date();
                newDate.setTime(dateInMilliSeconds + (timeInDay * num));
                return newDate;
            };
            return this; //this
        };
    };

如果沒有return語句,則在new Time(date).add(1).day())day()視為由new Time()實例的add方法返回的值的方法。 如果add方法沒有返回具有day方法的對象,那么您肯定會出錯。

return this僅對於鏈接是必需的。 您的代碼可以正常工作

var time = new Time(date);
time.add(1);
time.day();

要么

var time = new Time(date).add(1);
time.day()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM