kbjr/node-cards

deck.add is not a function

Closed this issue · 3 comments

So I used the sample code for creating a new deck generator function, but the deck variable that gets passed to it as its first parameter apparently doesn't have an add method, so I get this error:

deck.add(new cards.Card(suit, value));
     ^
TypeError: undefined is not a function

The exact code I used is this:

var cards = require("cards");

cards.generators.fiveHundred = function(deck) {
    ['club', 'diamond', 'heart', 'spade'].forEach(function(suit) {
        [4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A'].forEach(function(value) {
            //Remove black 4s
            if ((suit == 'club' || suit == "spade") && value == 4)
                return;
            else
                deck.add(new cards.Card(suit, value));
        });
    });
};

// Create the new deck constructor and point it to our generator 
cards.Deck.createType('FiveHundred', 'fiveHundred');

module.exports = function(){
    return new cards.FiveHundred({jokers: 1});
};

And then I obviously called that constructor somewhere else in my code.

kbjr commented

Looks like its a bug in Deck.createType, it's not correctly assigning the prototype of the new constructor to Deck.

kbjr commented

gonna leave this open until i get a chance to publish a new version to npm.

I rediscovered this issue...

It looks like this does the trick... took a little bit to figure out...

Deck.createType = function(name, generator, constructor) {
    var new_type = exports[name] = function() {
        Deck.call(this, generator);
        if (typeof constructor === 'function') {
            constructor.apply(this, arguments);
        }
    };
    new_type.prototype = new Deck();
};