Skip to main content
clarify, not sure that's what OP meant by "playing with prototypes"
Source Link
Bergi
  • 1.7k
  • 9
  • 16

Looks like that Grid and Cell should have parent-child relationship. Is it normal that I'm creating child objects at parent class?

There is no reason for a Grid and Cell to have any inheritance relationship. A Cell is not a kind of Grid, nor is a Grid a kind of Cell.

I need somehow to give access to context var from Cell class without passing it via constructor.

Why? That's exactly how you should give a Cell access to the context. The constructor is the right way to do this. It's completely normal for a container class to pass needed context into its children.

I had tried to play with prototypes but can't figure out how to do that.

That'sIt's a bad design to put data on the prototype. You'reYou would effectively makingmake it impossible to have two independent Grids, if every single instance of Cell can only share access to one Grid's context via Cell's prototype. It's far better for each Grid to pass its context into all its Cells.

Looks like that Grid and Cell should have parent-child relationship. Is it normal that I'm creating child objects at parent class?

There is no reason for a Grid and Cell to have any inheritance relationship. A Cell is not a kind of Grid, nor is a Grid a kind of Cell.

I need somehow to give access to context var from Cell class without passing it via constructor.

Why? That's exactly how you should give a Cell access to the context. The constructor is the right way to do this. It's completely normal for a container class to pass needed context into its children.

I had tried to play with prototypes but can't figure out how to do that.

That's a bad design. You're effectively making it impossible to have two independent Grids, if every single instance of Cell can only share access to one Grid's context via Cell's prototype. It's far better for each Grid to pass its context into all its Cells.

Looks like that Grid and Cell should have parent-child relationship. Is it normal that I'm creating child objects at parent class?

There is no reason for a Grid and Cell to have any inheritance relationship. A Cell is not a kind of Grid, nor is a Grid a kind of Cell.

I need somehow to give access to context var from Cell class without passing it via constructor.

Why? That's exactly how you should give a Cell access to the context. The constructor is the right way to do this. It's completely normal for a container class to pass needed context into its children.

I had tried to play with prototypes but can't figure out how to do that.

It's a bad design to put data on the prototype. You would effectively make it impossible to have two independent Grids, if every single instance of Cell can only share access to one Grid's context via Cell's prototype. It's far better for each Grid to pass its context into all its Cells.

Post Migrated Here from stackoverflow.com (revisions)
Source Link
user229044
  • 853
  • 5
  • 15

Looks like that Grid and Cell should have parent-child relationship. Is it normal that I'm creating child objects at parent class?

There is no reason for a Grid and Cell to have any inheritance relationship. A Cell is not a kind of Grid, nor is a Grid a kind of Cell.

I need somehow to give access to context var from Cell class without passing it via constructor.

Why? That's exactly how you should give a Cell access to the context. The constructor is the right way to do this. It's completely normal for a container class to pass needed context into its children.

I had tried to play with prototypes but can't figure out how to do that.

That's a bad design. You're effectively making it impossible to have two independent Grids, if every single instance of Cell can only share access to one Grid's context via Cell's prototype. It's far better for each Grid to pass its context into all its Cells.