A class within a class?












3















Ideally I would like to do something like the following:



class Chess = {
constructor() {
this.board = ...;
...
};

class Square = {
constructor(row, col) {
this.row = row;
this.col = col;
};
};


My main motivation is that with the Chess and Square classes defined separately something like: (this referring to the Chess class)



this.empty(square)


could be shortened to



square.empty()


which is more readable and more concise.



Unfortunately I can't just make a



Square.empty()


method since the results depends on the information in the Chess class and



square.empty(chess)


is no real improvement.



The reason I have a Square class is that something like



square.up()


seems much nicer than something like



[row, col + 1]


Do you have a suggestion on how I would accomplish the above? Some way to write a class within a class or something else entirely?



EDIT:



Following the advice from likle and alex I did the following:



I added a context property to the Class Square



class Square = {
constructor(context, row, col) {
this.context = context;
this.row = row;
this.col = col;
};
};


Then redefined some methods from the Chess.prototype to the Square.protoype. For example:



// before
Chess.prototype.empty = function (square) {
return this.piece(square) === 0;
};

// after
Square.prototype.empty = function () {
return this.piece() === 0;
};


Which meant that every time I created a Square object I need to add context. For example:



new Square(3, 4); // before
new Square(this, 3, 4); // after
new Square(this.context, 3, 4); // sometimes like this


To make the code more readable I created the following method:



Chess.prototype.createSquare = function (row, col) {
return new Square(this, row, col);
};


So a Square object can sometimes be created with



this.createSquare(3, 4);









share|improve this question




















  • 4





    You can have the definitions separate and just have Chess instantiate instances of Square.

    – alex
    Nov 23 '18 at 14:02
















3















Ideally I would like to do something like the following:



class Chess = {
constructor() {
this.board = ...;
...
};

class Square = {
constructor(row, col) {
this.row = row;
this.col = col;
};
};


My main motivation is that with the Chess and Square classes defined separately something like: (this referring to the Chess class)



this.empty(square)


could be shortened to



square.empty()


which is more readable and more concise.



Unfortunately I can't just make a



Square.empty()


method since the results depends on the information in the Chess class and



square.empty(chess)


is no real improvement.



The reason I have a Square class is that something like



square.up()


seems much nicer than something like



[row, col + 1]


Do you have a suggestion on how I would accomplish the above? Some way to write a class within a class or something else entirely?



EDIT:



Following the advice from likle and alex I did the following:



I added a context property to the Class Square



class Square = {
constructor(context, row, col) {
this.context = context;
this.row = row;
this.col = col;
};
};


Then redefined some methods from the Chess.prototype to the Square.protoype. For example:



// before
Chess.prototype.empty = function (square) {
return this.piece(square) === 0;
};

// after
Square.prototype.empty = function () {
return this.piece() === 0;
};


Which meant that every time I created a Square object I need to add context. For example:



new Square(3, 4); // before
new Square(this, 3, 4); // after
new Square(this.context, 3, 4); // sometimes like this


To make the code more readable I created the following method:



Chess.prototype.createSquare = function (row, col) {
return new Square(this, row, col);
};


So a Square object can sometimes be created with



this.createSquare(3, 4);









share|improve this question




















  • 4





    You can have the definitions separate and just have Chess instantiate instances of Square.

    – alex
    Nov 23 '18 at 14:02














3












3








3








Ideally I would like to do something like the following:



class Chess = {
constructor() {
this.board = ...;
...
};

class Square = {
constructor(row, col) {
this.row = row;
this.col = col;
};
};


My main motivation is that with the Chess and Square classes defined separately something like: (this referring to the Chess class)



this.empty(square)


could be shortened to



square.empty()


which is more readable and more concise.



Unfortunately I can't just make a



Square.empty()


method since the results depends on the information in the Chess class and



square.empty(chess)


is no real improvement.



The reason I have a Square class is that something like



square.up()


seems much nicer than something like



[row, col + 1]


Do you have a suggestion on how I would accomplish the above? Some way to write a class within a class or something else entirely?



EDIT:



Following the advice from likle and alex I did the following:



I added a context property to the Class Square



class Square = {
constructor(context, row, col) {
this.context = context;
this.row = row;
this.col = col;
};
};


Then redefined some methods from the Chess.prototype to the Square.protoype. For example:



// before
Chess.prototype.empty = function (square) {
return this.piece(square) === 0;
};

// after
Square.prototype.empty = function () {
return this.piece() === 0;
};


Which meant that every time I created a Square object I need to add context. For example:



new Square(3, 4); // before
new Square(this, 3, 4); // after
new Square(this.context, 3, 4); // sometimes like this


To make the code more readable I created the following method:



Chess.prototype.createSquare = function (row, col) {
return new Square(this, row, col);
};


So a Square object can sometimes be created with



this.createSquare(3, 4);









share|improve this question
















Ideally I would like to do something like the following:



class Chess = {
constructor() {
this.board = ...;
...
};

class Square = {
constructor(row, col) {
this.row = row;
this.col = col;
};
};


My main motivation is that with the Chess and Square classes defined separately something like: (this referring to the Chess class)



this.empty(square)


could be shortened to



square.empty()


which is more readable and more concise.



Unfortunately I can't just make a



Square.empty()


method since the results depends on the information in the Chess class and



square.empty(chess)


is no real improvement.



The reason I have a Square class is that something like



square.up()


seems much nicer than something like



[row, col + 1]


Do you have a suggestion on how I would accomplish the above? Some way to write a class within a class or something else entirely?



EDIT:



Following the advice from likle and alex I did the following:



I added a context property to the Class Square



class Square = {
constructor(context, row, col) {
this.context = context;
this.row = row;
this.col = col;
};
};


Then redefined some methods from the Chess.prototype to the Square.protoype. For example:



// before
Chess.prototype.empty = function (square) {
return this.piece(square) === 0;
};

// after
Square.prototype.empty = function () {
return this.piece() === 0;
};


Which meant that every time I created a Square object I need to add context. For example:



new Square(3, 4); // before
new Square(this, 3, 4); // after
new Square(this.context, 3, 4); // sometimes like this


To make the code more readable I created the following method:



Chess.prototype.createSquare = function (row, col) {
return new Square(this, row, col);
};


So a Square object can sometimes be created with



this.createSquare(3, 4);






javascript class oop nested






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 12:19







David

















asked Nov 23 '18 at 14:01









DavidDavid

255




255








  • 4





    You can have the definitions separate and just have Chess instantiate instances of Square.

    – alex
    Nov 23 '18 at 14:02














  • 4





    You can have the definitions separate and just have Chess instantiate instances of Square.

    – alex
    Nov 23 '18 at 14:02








4




4





You can have the definitions separate and just have Chess instantiate instances of Square.

– alex
Nov 23 '18 at 14:02





You can have the definitions separate and just have Chess instantiate instances of Square.

– alex
Nov 23 '18 at 14:02












2 Answers
2






active

oldest

votes


















1














Currently, there are no nested classes. What you could do is to have to two separate classes, Chess and ChessSquare - and have a reference to the chess passed in the constructor of the ChessSquare and keep that stored as a property. This way you won't have to pass it in the methods of the ChessSquare:



  class ChessSquare = {
constructor(chess, row, col) {
this.chess = chess;
this.row = row;
this.col = col;
}

empty() {
// "this.chess" references the chess, and "this" references the square.
}
};


You probably want to create all instances of ChessSquare within the Chess class itself.






share|improve this answer































    0














    One may argue that JavaScript does not have completely nested classes -- there is no way for a class to use parent class scope, for instance, nor would it be meaningful for anything but parent class properties (static declarations), but a class in JavaScript is just an object like any other, so you may as well define one and refer to it with a property on another class:



    class Chess {
    }

    Chess.Square = class {
    };


    Yes, a name for a class is optional.



    Then you can do things like:



    new Chess.Square();


    And generally everything else you do with a class or objects of a class.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448096%2fa-class-within-a-class%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Currently, there are no nested classes. What you could do is to have to two separate classes, Chess and ChessSquare - and have a reference to the chess passed in the constructor of the ChessSquare and keep that stored as a property. This way you won't have to pass it in the methods of the ChessSquare:



        class ChessSquare = {
      constructor(chess, row, col) {
      this.chess = chess;
      this.row = row;
      this.col = col;
      }

      empty() {
      // "this.chess" references the chess, and "this" references the square.
      }
      };


      You probably want to create all instances of ChessSquare within the Chess class itself.






      share|improve this answer




























        1














        Currently, there are no nested classes. What you could do is to have to two separate classes, Chess and ChessSquare - and have a reference to the chess passed in the constructor of the ChessSquare and keep that stored as a property. This way you won't have to pass it in the methods of the ChessSquare:



          class ChessSquare = {
        constructor(chess, row, col) {
        this.chess = chess;
        this.row = row;
        this.col = col;
        }

        empty() {
        // "this.chess" references the chess, and "this" references the square.
        }
        };


        You probably want to create all instances of ChessSquare within the Chess class itself.






        share|improve this answer


























          1












          1








          1







          Currently, there are no nested classes. What you could do is to have to two separate classes, Chess and ChessSquare - and have a reference to the chess passed in the constructor of the ChessSquare and keep that stored as a property. This way you won't have to pass it in the methods of the ChessSquare:



            class ChessSquare = {
          constructor(chess, row, col) {
          this.chess = chess;
          this.row = row;
          this.col = col;
          }

          empty() {
          // "this.chess" references the chess, and "this" references the square.
          }
          };


          You probably want to create all instances of ChessSquare within the Chess class itself.






          share|improve this answer













          Currently, there are no nested classes. What you could do is to have to two separate classes, Chess and ChessSquare - and have a reference to the chess passed in the constructor of the ChessSquare and keep that stored as a property. This way you won't have to pass it in the methods of the ChessSquare:



            class ChessSquare = {
          constructor(chess, row, col) {
          this.chess = chess;
          this.row = row;
          this.col = col;
          }

          empty() {
          // "this.chess" references the chess, and "this" references the square.
          }
          };


          You probably want to create all instances of ChessSquare within the Chess class itself.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 14:17









          liklelikle

          1,16039




          1,16039

























              0














              One may argue that JavaScript does not have completely nested classes -- there is no way for a class to use parent class scope, for instance, nor would it be meaningful for anything but parent class properties (static declarations), but a class in JavaScript is just an object like any other, so you may as well define one and refer to it with a property on another class:



              class Chess {
              }

              Chess.Square = class {
              };


              Yes, a name for a class is optional.



              Then you can do things like:



              new Chess.Square();


              And generally everything else you do with a class or objects of a class.






              share|improve this answer




























                0














                One may argue that JavaScript does not have completely nested classes -- there is no way for a class to use parent class scope, for instance, nor would it be meaningful for anything but parent class properties (static declarations), but a class in JavaScript is just an object like any other, so you may as well define one and refer to it with a property on another class:



                class Chess {
                }

                Chess.Square = class {
                };


                Yes, a name for a class is optional.



                Then you can do things like:



                new Chess.Square();


                And generally everything else you do with a class or objects of a class.






                share|improve this answer


























                  0












                  0








                  0







                  One may argue that JavaScript does not have completely nested classes -- there is no way for a class to use parent class scope, for instance, nor would it be meaningful for anything but parent class properties (static declarations), but a class in JavaScript is just an object like any other, so you may as well define one and refer to it with a property on another class:



                  class Chess {
                  }

                  Chess.Square = class {
                  };


                  Yes, a name for a class is optional.



                  Then you can do things like:



                  new Chess.Square();


                  And generally everything else you do with a class or objects of a class.






                  share|improve this answer













                  One may argue that JavaScript does not have completely nested classes -- there is no way for a class to use parent class scope, for instance, nor would it be meaningful for anything but parent class properties (static declarations), but a class in JavaScript is just an object like any other, so you may as well define one and refer to it with a property on another class:



                  class Chess {
                  }

                  Chess.Square = class {
                  };


                  Yes, a name for a class is optional.



                  Then you can do things like:



                  new Chess.Square();


                  And generally everything else you do with a class or objects of a class.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 14:23









                  amnamn

                  3,95053162




                  3,95053162






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448096%2fa-class-within-a-class%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Berounka

                      Sphinx de Gizeh

                      Different font size/position of beamer's navigation symbols template's content depending on regular/plain...