• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

passing params to constructor in partial classes and super, javascript

April 10, 2020 by

Questions › passing params to constructor in partial classes and super, javascript
0
Vote Up
Vote Down
Garmaine asked 4 years ago

Hay..

Current Situation

I have a function to render 9 Squares in a 3×3 grid where in each Square 9 numbers are generated also in a 3×3 grid.

Along with this i store the created DOM-Nodes in 3 Javascript classes: Grid, Square and Field. Square extends Grid and Field extends Square and Grid.

Wanted Situation

In the end I want that Grid holds an Array of all Squares and an Array of all Fields. Squares and fields should inherit those Arrays then. I require this for a Sudoku generator.

At the moment each Square has all Squares and Each field has all Fields and all Squares. Grid only its own Element and only its own Class.

Is there any way to have a variable in Grid holding Squares and Fields, and everytime a new Field or Square gets constructed, Grid updates this variable? Or is this nesting all wrong?


Grid.js

export class GridClass
{
    gridElem;

    constructor()
    {
        this.gridElem = this.getGridElem();
        console.log(this)
    }

    getGridElem = () => document.getElementById("grid");

}

export let GridInstance = new GridClass();

Square.js

import {GridClass} from "./Grid";

export class SquareClass extends GridClass
{
    squareElem;
    squares;
    squareID;

    constructor(squares, squareElem, squareID, gridElem)
    {
        super(gridElem);
        this.squareID = squareID;
        this.squares = squares;
        this.squareElem = this.getSquareElem();
    }

    getSquareElem = () => {
        return document.getElementsByClassName("square")[this.squares.length];
    }
}

Field.js

import {SquareClass} from "./Square";

export class FieldClass extends SquareClass
{
    fieldElem;
    fields;
    squareElem;

    constructor(gridElem, fieldElem, fields, squares, squareID)
    {
        super(gridElem, squares, squareID);
        console.log(squareID)
        this.fieldElem = fieldElem;
        this.fields = fields;
        this.squareElem = document.getElementsByClassName("square")[this.squares.length-1];
    }
}

Renderer.js

import {SquareClass} from "../dom/Square.js";
import {FieldClass} from "../dom/Field.js";

class RendererClass 
{
    squares;
    fields;

    constructor()
    {
        this.squares = new Array();
        this.fields = new Array();
    }

    renderGrid = () => {
        let squareJSON = {
            nodeName: "div",
            classes: ["square", "grid"],
        }
        let fieldJSON = {
            nodeName: "div",
            textNode: "0",
            classes: ["field"],
        }

        for(let squareCount=0; squareCount<9; squareCount++){
            let squareElement = this.createElement(document.getElementById("grid"), squareJSON);
            this.getSquarePosition(squareCount);
            this.squares.push(new SquareClass(this.squares, squareElement, squareCount));

            for(let fieldCount=0; fieldCount<9; fieldCount++) {
                let fieldElement = this.createElement(document.getElementsByClassName("square")[squareCount], fieldJSON);
                this.fields.push(new FieldClass(this.squares, fieldElement, this.fields))
            }
        }
    }
}
Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: constructor, javascript, node.js, sudoku, super

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Copyright © 2023