T
T
Taras Labiak2014-11-19 11:15:00
JavaScript
Taras Labiak, 2014-11-19 11:15:00

What is the correct way to refer to the setter of the parent class in the child in JavaScript?

If I declare a property in the prototype of the parent class, in the following way:

Foo.prototype = {
    get target() {
        return this._target;
    },

    set target(value) {
        this._target = value;
    }
}

How can I call the parent setter in the child class?
The only way I see is through the getOwnPropertyDescriptor call. If so, is this method costly in terms of performance, i.e. getOwnPropertyDescriptor. every time descriptor object and what is it worth?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Pushkarev, 2014-11-19
@rock

Yes, the only one. Yes, costly. If you don't like it, cache the parent descriptor.

var P = {
  set a(it){
    console.log('P');
  }
}
var C = {
  __proto__: P,
  set a(it){
    a.set.call(this, it);
    console.log('C');
  }
}
var a = Object.getOwnPropertyDescriptor(P, 'a');
C.a = 42;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question