0

I have searched all over for a comprehensible answer to this very simple question and cannot seem to find one. As a primarily java programmer, this has been a very frustrating process.

For example, say I am trying to program a deck of cards. The way I would do this would be to have a 'Card' class in card.js which would look something like this:

function Card(value, suit){
  this.value = value;
  this.suit = suit;

}

and then a 'Deck' class in deck.js which would look something like this :

function Deck(){
  this.cardArray = [];
  this.topCard = new Card(2, 'clubs');
}

Deck.prototype.shuffle = function(){
    //shuffle the deck
}

The problem here is that I get an error saying 'Unexpected Identifier'. Presumably because js doesn't realize that I have defined the Card class. How can I make it so the deck.js file can access the Card class?

I should mention that I am doing attempting to do this without a browser, so I think I would then be using node.js (Again, sorry I'm new to this environment). Or perhaps better stated, this will be server side.

4
  • 1
    If you are using node.js, then you probably want to look into the module system: nodejs.org/api/modules.html Commented Jul 19, 2017 at 18:09
  • 1
    FWIW, Deck.prototype.shuffle = function(){} should not be inside the constructor. Commented Jul 19, 2017 at 18:12
  • 'Unexpected Identifier' is a syntax error. The code you posted doesn't throw that error, so you seem to have omitted the part that contains that error. To be clear: If you are not importing Card into wherever Deck is defined then you will still get an error, but that would be a different error. Commented Jul 19, 2017 at 18:14
  • @FelixKling shoot my bad, I was typing this up quickly, i'll edit it. Thanks! Commented Jul 19, 2017 at 18:14

2 Answers 2

1

You can use the module system

In your card.js:

const Card = function(value, suit){
 this.value = value;
 this.suit = suit;
}
module.exports = Card;

And in your deck.js

const Card = require('./card');

function Deck(){
 this.cardArray = [];
 this.topCard = new Card(2, 'clubs');

}

 Deck.prototype.shuffle = function(){
   //shuffle the deck
 }
Sign up to request clarification or add additional context in comments.

1 Comment

You should not use that arrow function. "this" is not referencing the instance.
1

You need to use exports and imports:

In the card file, you would do:

function Card(num, suit) {
    this.num = num;
    this.suit = suit
}

module.exports = Card;

And then in the Deck file, you would do:

var Card = require('./Card.js');

function Deck() {
    this.cardArray = [];
    this.topCard = new Card(2, 'clubs');
}

Deck.prototype.shuffle = function () {
    //shuffle the deck
};

4 Comments

It's giving me an error saying "Uncaught ReferenceError: module is not defined". Do I have to define it somewhere first?
@TravisPavletich Are you using node to run this? Something like $ node Deck.js
yes I am. I found the problem I just mentioned and fixed it. But it is still giving me an error. It says on the line this.topCard = new Card(2, 'clubs'); Syntax error: Unexpected Identifier.
@TravisPavletich: That line should not produce this error. Do you have other code above it than what you posted here? Otherwise, the only explanation would be that the line contains an invalid non-printable character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.