0

Hi if I try to pass an array to a class function as a parameter it workes fine. try it in this snippet of code:

class boo {
    moo() {
        this.bob = [false, "Bob"];
        this.kees = [false, "Kees"];
    }

    doo() {
        this.bob = [true, "Bob"];
        this.kees = [true, "Kees"];
    }
}

class foo {
    constructor() {
        this.hi = [];
    }
    lala(arg) {
        let bool = arg[0];
        let string = arg[1];
        if (bool && !this.hi[string]) {
            this.hi[string] = true;
            return string + " is true"
        }
        return string + " is true - will be entered one time not multiple";
    }

    dada(arg) {
        let bool = arg[0];
        let string = arg[1];
        if (!bool && this.hi[string]) {
            this.hi[string] = false;
            return string + " is false";
        }
        return string + " is false - will be entered one time not multiple";
    }
}

objBoo = new boo();
objFoo = new foo();

objBoo.doo();
console.log(objFoo.lala(objBoo.bob));
console.log(objFoo.lala(objBoo.bob));
console.log(objFoo.lala(objBoo.kees));
objBoo.moo();
console.log(objFoo.dada(objBoo.bob));
console.log(objFoo.dada(objBoo.kees));
console.log(objFoo.dada(objBoo.kees));

So what is the problem in this code snippet? I can not figure it out by my self. I feel like it has something to do with this question: How to access the correct this inside a callback?

/*ignore pls*/class BehaviourClass {
/*ignore pls*/    constructor() {
/*ignore pls*/        this.start = true;
/*ignore pls*/    }
/*ignore pls*/
/*ignore pls*/    Awake() {
/*ignore pls*/        window.AnimationFrame();
/*ignore pls*/    }
/*ignore pls*/}

//like the boo class where it works fine;
class KeyCodeClass {
    KeyDown(e) {
        //spacebar
        if (e.keyCode == 32) { this.Space = [true, "Space"] }
    }

    KeyUp(e) {
        //spacebar
        if (e.keyCode == 32) { this.Space = [false, "Space"] }
    }
}

//like the foo class where it works fine
class InputClass {
    constructor() {
        this.pressed = [];
    }

    GetKeyDown(arg) {
        let bool = arg[0];
        let string = arg[1];
        if (bool && !this.pressed[string]) {
            this.pressed[string] = true;
            return true;
        }
        if (!bool && this.pressed[string]) {
            this.pressed[string] = false;
            return false;
        }
    }

    GetKeyUp(arg) {
        let bool = arg[0];
        let string = arg[1];
        if (bool && !this.pressed[string]) {
            this.pressed[string] = true;
            return false;
        }
        if (!bool && this.pressed[string]) {
            this.pressed[string] = false;
            return true;
        }
    }
}

/*ignore pls*/var Behaviour = new BehaviourClass();
var KeyCode = new KeyCodeClass();
var Input = new InputClass();

//controles if a key is pressed
window.addEventListener("keydown", KeyCode.KeyDown.bind(KeyCode), false);
window.addEventListener("keyup", KeyCode.KeyUp.bind(KeyCode), false);

/*ignore pls*/function AnimationFrame() {
/*ignore pls*/    Update();
/*ignore pls*/    requestAnimationFrame(AnimationFrame);
/*ignore pls*/}

function Update() {
    if(Input.GetKeyDown(KeyCode.Space)) {
        console.log("spacebar down");
    }
    if(Input.GetKeyUp(KeyCode.Space)) {
        console.log("spacebar up");
    }
}

/*ignore pls*/Behaviour.Awake();

I get the error arg is undefined by the second snippet. But in the class foo from the first snippet works fine. Can someone explain what I do wrong. The snippets are in my opinion almost the same.

TessavWalstijn
  • 1,698
  • 1
  • 19
  • 36

1 Answers1

0

KeyCode.Space is not defined in your code, so the value of KeyCode.Space is undefined and when you call the function Input.GetKeyDown(KeyCode.Space) the argument is passed as undefined.

Next, in the function GetKeyDown(arg), the arg would be undefined and when you are trying to find the element at 0th index by using the statement let bool = arg[0]; it throws an error

TypeError: Cannot read property '0' of undefined

So, define the value for the KeyCode.Space in your code and also write the gaurd condition before proceeding ahead in the function. If the condition fails, then return.

Abhinav Galodha
  • 9,293
  • 2
  • 31
  • 41