We help IT Professionals succeed at work.

Javascript object functions help

Primoz Ivancic
on
111 Views
Last Modified: 2017-04-06
Hi all,

I'm new to javascript and I have no idea what I'm doing wrong. I have created a mazeObject fine, and would like to hold an array of mazeTile inside it, but when I try to define the objects in the array it creates them as a function rather than as the object.

mazeObject and instancing:
var mazeObject = function () {

    var constructor = function mazeObject(width, height) {
        var width;
        var height;
        var tiles;

        this.width = width;
        this.height = height;

        this.resize = function (x, y) {
            this.tiles = [];

            // Creates all lines:
            for (var i = 0; i < x; i++) {

                // Creates an empty line
                this.tiles.push([]);

                // Adds cols to the empty line:
                this.tiles[i].push(new Array(y));
            }

            this.clear()
        }

        this.clear = function () {
            for (var i = 0; i < this.width; i++)
                for (var j = 0; j < this.height; j++) {
                    this.tiles[i][j] = new mazeTile();
                }
        }

        this.getTile = function (x, y) {
            return this.tiles[x - 1, y - 1];
        };

        this.setTile = function (x, y, value) {
            this.tiles[x - 1, y - 1].contents = value;

        }

        // Initialize the tiles
        this.resize(width, height);
        this.setTile(1, 1, tcDiamondDoor);
    };

    return constructor;
}();
var maze = new mazeObject(7, 5);

Open in new window


mazeTile (instancing in mazeObject.clear)
var mazeTile = function () {

    var constructor = function mazeTile() {
        var topWall = twNone;
        var leftWall = twNone;
        var otherWall = twoNone;
        var contents = tcNone;

        this.getContents = function () {
            return contents;
        }

    }

    return constructor;
}

Open in new window


When maze.clear gets called and it creates the instance of all the mazetile objects, those tiles are of prototype function(), while the maze itself is something different.

And later when I try to use/read the tiles javascript crashes with no error (tried with just .contents or a function getcontents):
            if (tileRectContents[maze.getTile(i, j).contents] != tcNone)
            {
                drawImage(ctx, imgSpriteTile, tileRectContents[maze.getTile(i, j).getContents()], targetRect);
            }

Open in new window


Any help would be appreciated as google has not been helpful
Comment
Watch Question

This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions