0

I created this obj:

const obj = {
  myVar: "123456",

  getVar: () => {
    return this.myVar;
  },

  setVar: newVal => {
    this.myVar = newVal;
  }
};

And when I set a new value, like this:

obj.setVar(666333);

The console prints this:

console.log("current value:::", obj.getVar());
// current value::: 666333

100% correct!

But the error happens when I try to print the initial value (123456) without set a new value before:

console.log("current value:::", obj.getVar());
// current value::: undefined <-- Why undefined??
// expected value: 123456

Where am I wrong?

0 Answers0