Ivan
Ivan

Reputation: 895

Typescript `super` calling from overridden method

Recently found issue in my project, here is example code (angular 2.3 context):

export class HttpService extends Http {
    ...
    request(url: string, requestOptions?: RequestOptionsArgs, config: any = {}) { // method overridden
        ...
        return super.request(url, options) // called Http.request()
     }
    getOptions() {
        ...
        super.request(url, options) // called this.request()
    }
}

Can someone explain why in 1st case called method from super as expected, but in 2nd case called from this (found in debug console)?

Upvotes: 1

Views: 1004

Answers (1)

Sebastian Sebald
Sebastian Sebald

Reputation: 16856

super still follows the prototypical-inheritance rules. Meaning it will try to find request first in the HttpService.prototype and only if it doesn't find it there go down the prototype chain.

Here is some more information about this behavior: http://2ality.com/2015/02/es6-classes-final.html#referring-to-super-properties-in-methods

Upvotes: 1

Related Questions