K
K
Konstantin2020-09-28 18:15:44
JavaScript
Konstantin, 2020-09-28 18:15:44

Why do I get undefined when accessing the parent property?

Parent:

class a {

protected _selected: any;
      
   get selected() {
        return this._selected;
    }

    set selected(value: any) {
        this._selected = value;
    }
}


Child:

class b extends a {
    set selected(value: any) {
        super.selected = value;
        
    }

get selected() {
      return super.selected;
}
}


Call:

let _b = new b();
_b.selected = "333";
console.log(_b.selected); // 333


Why - if not override in child:

get selected() {
        return this._selected;
    }


Then this property is not looked up in the parent, returns undefined when tried . console.log(_b.selected);

Found a discussion here

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2020-09-28
@Junart1

In the original, the child was without a getter, the value of the child overlapped the value of the parent, this is how prototypal inheritance works.
In your version, everything works, since selected is declared with both a getter and a setter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question