W
W
WarriorKodeK2018-02-23 13:04:04
JavaScript
WarriorKodeK, 2018-02-23 13:04:04

How to link objects?

Hello.
The question is more architectural.
Let's say I have Player and Card objects.
Looks like this:

class User{
  constructor(id, name){
  	this.userId = id;
    this.name = name;
  }
}

class Card{
  constructor(){
  	Object.setPrototypeOf(Card.prototype, gameSetting);
  }
  createCard(){
  	this.card	= document.createElement('div');
    //.....
  }
  
  //Some methods here
}

I have a button Create new Cardthat creates a card (new Card()) in the DOM (there may be many of them).
Question:
How can I link a card with a user, so that later, for example, when clicking on a card, there will be a alert "This card belong to user${userId}".
Perhaps somehow by Id?
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
abasiuk, 2018-02-23
@WarriorKodeK

Add the userId property to the card.

D
dmnmkua, 2018-02-23
@dmnmkua

And why not do it with one class, since when creating a card, a link to a specific user is created.
Well, or then link them with inheritance through extends.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question