javascript - How to access parent object in JS? -


i'm novice programmer , ran oop problem while programming game in javascript:

it's bughouse chess game. it's played 4 players on 2 chess boards: each board independant chess game between members of opposing teams, white player in 1 board in team black player on other board. when captures piece, passed teammate can place anywhere on board instead of playing normal chess move. bughouse chess - wikipedia

i have bughousematch object contains 2 properties of type bughousegame - these 2 games being played simultaneously.

the bughousegame object inherits game, regular independant chess game. bughousegame contains property "link", set reference other bughousegame. allowes passing pieces between games , communicating victory achieved in 1 of games.

it contains property players, array of 2 players (of type player) of opposing teams, 1 playing white , other black. problem encountered don't know correct way keep score of match - put property keeps number of wins of each team?

it seems natural score kept in bughousematch, can't access bughousegame since it's property of bughousematch. ended doing creating score object referenced 2 teammates in each team (the black player in 1 board , white in other), in there's property keeps number of wins of team. never did - 2 objects point same instance of object, , feels mistake. it's easier understand diagram made: image of relations between objects

i'd love hear opinion ideas of making "link" property in each bughousegame, , making shared "score" object in players teammates. also, if have idea of how problem solved in c#, proper oop language, i'd love hear well. in advance!

focusing on question title of 'how access parent object in js?' there few options. can use jquery:

 $(this).closest('selector'); 

or plain js (you can use document method):

parent.getelementbyid('id'); 

if have use js , need traverse hierarchy can stuff this, though don't recommend it:

parent.parent.parent.method(); 

also: jquery parent of parent


Comments