我一直在用BDD驱动的方式编写一些模块,我选择了这种模式。问题是我的Deck Setup模块中的方法需要实例化并从exports.Deck调用方法。我该怎么做?
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();
};
答案 0 :(得分:1)
如果Deck_Setup需要从Deck访问公开的方法,请使用require
:
var deck = require('./Deck')
deck
变量将是new constructor
您也可以通过将设置和Deck工厂设置为同一模块来实现它(您可以导出对象:
var Deck = {}
var Deck_Setup = {}
module.exports = {
Deck : Deck,
Deck_Setup : Deck_Setup
};
您可能应该阅读此博文并找到自己的方式:http://bites.goodeggs.com/posts/export-this/