0

let mario = {
    x: 20,
    w: 20,
    h: 40, 
    y: canvas.height-mario.h, 
    speedX: 0,
    speedY: 10,
    dx: 4,
    dy: -7,
    color: "black",
    canJump: true,
    onGround: true,
}


export default mario

when i try the following code i get error as

player.js:5 Uncaught ReferenceError: Cannot access 'mario' before initialization
    at player.js:5:22

please help me and provide me insights

1 Answers1

0

Initialize mario with the literal values first, and add the computed value later:

let mario = {
    x: 20,
    w: 20,
    h: 40, 
    speedX: 0,
    speedY: 10,
    dx: 4,
    dy: -7,
    color: "black",
    canJump: true,
    onGround: true,
};
mario.y = canvas.height-mario.h;
export default mario;
Heiko Theißen
  • 12,807
  • 2
  • 7
  • 31
  • 1
    Please look for duplicates before answering. [This question is asked frequently](https://stackoverflow.com/questions/linked/4616202?sort=newest). – Ivar Aug 07 '23 at 13:52