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

Class inheritence: put call to parent constructor after "fat arrow" method declarations. #2488

Closed
sqlly opened this issue Mar 25, 2015 · 1 comment
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@sqlly
Copy link

sqlly commented Mar 25, 2015

Please consider the following code:

interface MenuItem{
    caption:string;
    handler: string;
    handlerFunction?: Function;
}
class BaseStuff{
    public menu:Array<MenuItem>=[];
    constructor (menu:Array<MenuItem>){
        this.menu=menu;
        this.prepareMenu(menu);
    }
    public prepareMenu(menu:Array<MenuItem>){
        for (var item in menu){
            item.handlerFunction=this[item.handler];
        }
    }
}

class BadStuff extends BaseStuff{
    public someAction=()=>{
        alert("SomeAction from BadStuff called");
    }
}

var bad=new BadStuff([{caption: "test", handler: "someAction"}]);

I need a class, that will send, say, menu structure to the parent where actual handler function will be found. The problem is that when methods is declared via fat arrow, the call to parent constructor goes before the actual method will be defined:

        _super.apply(this, arguments);
        this.someAction = function () {
            alert("SomeAction from BadStuff called");
        };

It looks logical to put first line of code including call to parent contstuctor AFTER all method declarations in the class.

@RyanCavanaugh RyanCavanaugh added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Mar 25, 2015
@RyanCavanaugh
Copy link
Member

There's no special thing called a "fat arrow method declaration". These are just instance members with initializers that happen to be function expressions.

Consider code like this:

class BaseStuff{
    public thing = function() { return 'hello world'; }
    constructor (){
    }
    public getHandler() {
        return this.thing;
    }
}

class BadStuff extends BaseStuff{
    public someOtherThing = this.getHandler();
}

var x = new BadStuff();
// Would fail in proposed initialization order
console.log(x.someOtherThing());

See also #1617.

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

2 participants