Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Call to super in child class constructor does not give any error #5317

Closed
zeeshanhanif opened this issue Oct 18, 2015 · 11 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@zeeshanhanif
Copy link

A call to super multiple times in Child class' constructor does not give any error, is it expected behavior? Dose it initiate multiple parent objects?

class E {
    name:string;
    constructor(theName: string,age:number) { 
        this.name = theName; 
        console.log("E constrcutor");
    }
    displayName():void {
        console.log(" Name = " + this.name);
    }
}
class F extends E {
    name:string;
    constructor(theName: string) {
        this.name = theName; 
        console.log("F constrcutor");
        super(theName,4);
        super("Hello",5); // No error here 
    }
}
let e: E = new E("E",1);
let f: F = new F("F");
f.displayName(); // return "Hello"
@zeeshanhanif zeeshanhanif changed the title Multiple Call to super in child class does not give any error Multiple Call to super in child class constructor does not give any error Oct 18, 2015
@wgebczyk
Copy link

you cannot use this before super call...
... and of course second super call makes no sense.

@zeeshanhanif
Copy link
Author

Actually 'this' is working before super call, you can call super anywhere in the constructor

@wgebczyk
Copy link

@zeeshanhanif OK, your statement has surprised me. My understanding is that this usage in constructor is prohibited under super call is fully resolved. Could you point me to specification of ES6 that allows that free this usage in derived class constructor with super somewhere in the middle?

@kitsonk
Copy link
Contributor

kitsonk commented Oct 18, 2015

Well, while not the ES6 Specification, MDN shows clearly that is valid. The specification makes no assertions to the positioning of the super call (or the number of times) as well.

@wgebczyk
Copy link

Can you reread Description section from your first link (MDN) the one after Syntax and before Example?
I'm reading it exactly as I've suggested:

When used in a constructor, the super keyword appears alone and must be used before the this keyword can be used. This keyword can also be used to call functions on a parent object.

@zeeshanhanif
Copy link
Author

@wgebczyk
Example in MDN also says calling 'this' before super will cause reference error.
But example I have provided above is working fine with typescript and does not give any error.

@wgebczyk
Copy link

OK, we misunderstood each other. You are saying that now it works, but it should not. :) I tried to say the same thing :)

@zeeshanhanif
Copy link
Author

Yes, if we are following object oriented style like Java or C# then super can only be called in first line of constructor and only at once. But I'm not sure if current behavior in typescript is expected or not

@wgebczyk
Copy link

In [https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals] in Goals in point 6:
Align with current and future ECMAScript proposals.
So it seems that expected is to disallow this usage before super call.

@zeeshanhanif
Copy link
Author

Ok, means we should expect that it will be restricted in future.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 19, 2016

this should be handled by #4211

@mhegazy mhegazy closed this as completed Feb 19, 2016
@mhegazy mhegazy added the Duplicate An existing issue was already created label Feb 19, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants