문제

I have been writing some modules in a BDD driven way and I chose this type of pattern. The problem is that a method in my Deck Setup module needs to instantiate and call methods from exports.Deck. How would i do this?

exports.Deck_Setup = function() {
    var ShuffledDeck=[];

    var card_dummy= [
        {
            Rank:"Queen",
            Suit: "Hearts"
        },
        {
            Rank:"King",
            Suit: "Hearts"
        },
        {
            Rank:"Ace",
            Suit: "Spades"
        }
    ];

    var constructor = function() {};
        constructor.prototype.generateDECK = function(Card, DeckBuilder){
            var randomNumber = Math.floor(Math.random() * 3);
            console.log(Card, DeckBuilder);
            test = new Card();
            // var new1 = Card.add(card_dummy[0]);          
            // card = new Card(card_dummy[randomNumber]);

        };
            // return (Cards.indexOf(Card) > -1) ? true : false;

    return new constructor();

};

exports.Deck = function() {
    var Cards=[];

    var constructor = function() {};
        constructor.prototype.add = function(Card){
            if (this.size() >= 52){
                return false; //can't add anymore cards
            }
            else {
                Cards.push(Card);
                return true;
            }
        };

        constructor.prototype.remove = function (){
            if (this.size() > 0){
                Cards.pop();
                return true             
            }
            return false;
        };

        constructor.prototype.size = function() {
            return Cards.length;
        };

        //magic method for our cards
        constructor.prototype.includes = function(Card) {
            for (var each in Cards){
                if (JSON.stringify(Cards[each]) === JSON.stringify(Card) ){
                    return true;
                }
            }
            return false;
        };
            // return (Cards.indexOf(Card) > -1) ? true : false;

    return new constructor();

};
도움이 되었습니까?

해결책

If the Deck_Setup needs to access exposed methods from Deck, use require :

var deck = require('./Deck')

where thedeck variable will be the result of your new constructor

You can also achieve it by setting both the setup and the Deck factory into the same module (you can export an object:

 var Deck = {}
 var Deck_Setup = {}
 module.exports = {
    Deck : Deck,
    Deck_Setup : Deck_Setup
 };

May be you should read this blog post and find your own way: http://bites.goodeggs.com/posts/export-this/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top