Bug? I can’t add the parameter in Javascript

Asked

Viewed 40 times

-1

Good morning.

So, on the question, I’m trying to refactor my code to OO and I have the following problem:

(this is a minimum problem code that reproduces the problem)

index.html

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Um título legal</title>
</head>
<body>
    <button id="btn-click">Click</button>
    <script type="module" src="./main.js"></script>
</body>
</html>

Test js.

class Test {
  add(level) {
    level += 1;
  }
}

export default new Test();

main.js

import Test from "./Test.js";
let level = 1;


document.getElementById("btn-click").addEventListener("click", () => {
    Test.add(level);
    console.log(level);
});

Would anyone know a possible solution?

EDIT1: It was to add level 1 when calling Test.add(level), but that’s not what happens.

  • Where and exactly what your mistake is?

  • Guy opens the Chrome developer tools and sees if there’s any CQRS error think your script main.js is not being loaded.

1 answer

0

Solution:

index.html

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Um título legal</title>
</head>
<body>
    <button id="btn-click">Click</button>
    <script type="module" src="./main.js"></script>
</body>
</html>

Test js.

class Test {
  add(levelObject) {
    levelObject.level += 1;
  }
}
export default new Test();

main.js

import Test from "./Test.js";
let levelObject = {
    level: 1,
};

document.getElementById("btn-click").addEventListener("click", () => {
    console.log(levelObject.level);
    Test.add(levelObject);
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.