Class method shorthand return

# somonek (7 years ago)

I this return shorthand would be helpful.

class MyClass { deleteProfileImage() => (....) }

as we already have this

const myFunction = () => (....);

any thoughts?

Serghei

# Brian Ninni (7 years ago)

I think it might be problematic, since (if it works the same way as arrow functions currently do) 'this' will refer to the context in which the class was defined, and not to the instance that the function is attached to. And if it doesn't need to access the instance, then it doesn't really need to be an instance method anyway.

However if there was a shorthand for regular functions, then this still might be solvable by allowing key : value pairs in the class definition. It's only one more character than your proposal (the ':'), and would allow for values other than shorthand functions to be defined

class MyClass { deleteProfileImage : () -> (...) //or some other new shorthand for

regular functions isMyClass : true static myStaticMethod : () =>(...) }

instead of

class MyClass { deleteProfileImage(){ return ... } } MyClass.prototype.isMyClass = true MyClass.myStaticMethod = () => (...)

# T.J. Crowder (7 years ago)

On Fri, May 5, 2017 at 6:49 PM, Brian Ninni <ninni.brian at gmail.com> wrote:

I think it might be problematic, since (if it works the same way as arrow functions currently do) 'this' will refer to the context in which the class was defined, and not to the instance that the function is attached to.

Not if we make it consistent with public class fields (and we'd want to). Within the class structure, this refers to the instance (in that proposal at least). Which is consistent with method syntax and Serghei's suggestion here.

This is in significant use in the React community already, via transpiling, e.g. this works:

class Widget extends React.Component {
    handleClick = _ => {
        this.setState(state => ({counter: state.counter + 1}));
    };
    render() {
        return <div onClick={this.handleClick}>{this.state.counter}</div>;
    }
}

The public class fields stuff also weakens the case for Serghei's concise method syntax, since you can do the above.

-- T.J. Crowder