1

I want to compose multiple objects into one. The super keyword should call corresponding parent object method. If parent's method is not present, it should call next parent's method and so on.

Example code that throws:

var base = {
  runBusinessLogic() {
    console.log('BASE');
  }
}

var concrete = {

  unrelatedExtraMethod() {
  }
}

var addon = {
  runBusinessLogic() {
    super.runBusinessLogic(); // I expect this super call to call base.runBusinessLogic(..)
    console.log('ADDON');
  }
}

var layer2 = Object.setPrototypeOf(concrete, base);
var layer3 = Object.create(layer2, addon);

layer3.runBusinessLogic(); // throws "TypeError: layer3.runBusinessLogic is not a function"

I expect the layer3.runBusinessLogic(..) to call base.runBusinessLogic(..) and still have callable unrelatedExtraMethod on itself.

How to make the super keyword work as outlined?

1

1 Answer 1

1

    var base = {
      runBusinessLogic() {
        console.log('BASE');
      }
    }

    var concrete = {

      unrelatedExtraMethod() {
      }
    }

    var addon = {
      runBusinessLogic() {
        super.runBusinessLogic(); // I expect this super call to call base.runBusinessLogic(..)
        console.log('ADDON');
      }
    }

    var layer2 = Object.setPrototypeOf(concrete, base);
    var layer3 = Object.setPrototypeOf(addon, layer2); // change this

    layer3.runBusinessLogic()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.