0

I am having hard time calling a function inside a class. When I started working on this, I had it running, but after making revisions, I cant get it working :((
You can ignore my alerts because I just put them in there to check to see if the function gets called or not.
I have function "Unit" which is a class for me. then I call it by below.
From below code, that new Unit(args) does not get completed. It stops at this.direction=this.setDirection(); and this.setDirection() is not called for some reason. Can anyone take a look and tell me what went wrong?
Thanks!


var teamBlack = [];

var bking = new Unit("black", "king", new Coords(3,1, false));
teamBlack.push(bking);

function Unit(team, type, coords) {

    alert("Unit started " + count);
    this.team = team;
    this.type = type;

    this.position = coords;
    this.highlight = false;
    alert("before setdirection");
    this.direction = this.setDirection();
    alert("this " + this.type);

    this.setDirection = function () {
        alert("setDirection Started ");
        alert(this.type);
        var tempDir = [];
        switch (this.type) {
            case "king" :
                alert("this is king");
                tempDir = [this.N(), this.NW(), this.W(), this.SW(), this.S(), this.SE(), this.E(), this.NE()];
                break;
            case "bishop" :
                tempDir = [this.NW(), this.SW(), this.SE(), this.NE()];
                break;
            case "rook" :
                tempDir = [this.N(), this.S(), this.W(), this.E()];
                break;
            case "pawn" :
            {
                if (this.team == "white") {
                    tempDir = [this.S()];
                } else {
                    tempDir = [this.N()];
                }
                break;
            }
            case "queen" :
            {
                if (this.team == "white") {
                    tempDir = [this.N(), this.W(), this.SW(), this.S(), this.SE(), this.E()];
                } else {
                    tempDir = [this.N(), this.NW(), this.W(), this.S(), this.E(), this.NE()];
                }
                break;
            }

        }
        tempDir = tempDir.filter(function (dir) {
            return dir.x > -1 && dir.x < 3 && dir.y > -1 && dir.y < 4 && dir.c == false;
        });

        return tempDir;
    }
}
1
  • 1
    You are calling this.setDirection before you have assigned a value to it. You can't do that. Move it to afterward. You may have heard about function declarations being "hoisted", but your assignment to setDirection isn't a declaration, it's a simple assignment of a function to an object property. Commented Apr 11, 2016 at 5:52

4 Answers 4

2

This code is wrong because you try call function that is not exist yet in your class. I recommend you to move this function in prototype. For example:

function Unit() {
    //this is your constructor
}

Unit.prototype.setDirection = function() {
    //your code of setDirection function
}
Sign up to request clarification or add additional context in comments.

Comments

0

Make sure variables are defined, then put this.setDirecton = function()... before calling this.setDirection().

See JSFiddle.

Comments

0

You need to use the setDirection function as a prototype of your Unit class.

Unit.prototype.setDirection = function() {
    //setDirection functionality
}

Within the prototype you have access to your class properties via this.

Check this jsFiddle: https://jsfiddle.net/urtr3quc/

Comments

0

I would improve your code by separating the responsibilities:

//create the class Unit

    function Unit(team, type, coords) {

        alert("Unit started " + count);
        this.team = team;
        this.type = type;

        this.position = coords;
        this.highlight = false;
    }

Then you specify new method for your class:

Unit.prototype.setDirection = function () {
        alert("setDirection Started ");
        alert(this.type);
        var tempDir = [];
        switch (this.type) {
            case "king" :
                alert("this is king");
                tempDir = [this.N(), this.NW(), this.W(), this.SW(), this.S(), this.SE(), this.E(), this.NE()];
                break;
            case "bishop" :
                tempDir = [this.NW(), this.SW(), this.SE(), this.NE()];
                break;
            case "rook" :
                tempDir = [this.N(), this.S(), this.W(), this.E()];
                break;
            case "pawn" :
            {
                if (this.team == "white") {
                    tempDir = [this.S()];
                } else {
                    tempDir = [this.N()];
                }
                break;
            }
            case "queen" :
            {
                if (this.team == "white") {
                    tempDir = [this.N(), this.W(), this.SW(), this.S(), this.SE(), this.E()];
                } else {
                    tempDir = [this.N(), this.NW(), this.W(), this.S(), this.E(), this.NE()];
                }
                break;
            }

        }
        tempDir = tempDir.filter(function (dir) {
            return dir.x > -1 && dir.x < 3 && dir.y > -1 && dir.y < 4 && dir.c == false;
        });
        alert("before setdirection");
        this.direction = tempDir;//setMethod doesn't return thing but set something somewhere
        alert("this " + this.type);
    };

and now you invoke your class instance:

var teamBlack = [];

var bking = new Unit("black", "king", new Coords(3,1, false));
bking.setDirection();
teamBlack.push(bking);

I suggest you to take a look on this link

1 Comment

after your lines.. sorr I forgot to type it.. fixing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.