Skip to content Skip to sidebar Skip to footer

Unable To Implement Module Pattern

I am trying to reproduce some code from the book 'Javascript: The Good Parts' by Douglas Crockford. The idea is to use closures for object encapsulation and avoid Javascript's inhe

Solution 1:

You are trying to execute the result of a function as a function, and are assigning values to functions. Try:

var seqer = serial_maker;
seqer.set_prefix('Q');
seqer.set_seq(1000);
var unique = seqer.gensym();

Also see this jsFiddle


Solution 2:

There are two errors in this code example:

  1. The definition of serial_maker is finished with () which invokes the anonymous function. That makes the next line:

    var seqer = serial_maker();
    

    erroneous since serial_maker is not the function but the object returned by the anonymous function.

  2. Once the previous error is fixed the two lines:

    seqer.set_prefix = 'Q';
    seqer.set_seq = 10000;
    

    should change to:

    seqer.set_prefix('Q');
    seqer.set_seq(10000);
    

(Source: http://oreilly.com/catalog/errata.csp?isbn=9780596517748&order=date)


Solution 3:

I am currently working through the book and I see a redundant pair of parentheses () in your posted code, when I compare it to the book. You have:

        }
    };
}(  );

it should be:

        }
    };
};

Along with, the additional answers where the 'Q' and the 1000 need to be wrapped in ().


Post a Comment for "Unable To Implement Module Pattern"