ES7 property initializers

# Benjamin (Inglor) Gruenbaum (10 years ago)

Looking at the syntax from here:

facebook.github.io/react/blog/2015/01/27/react-v0.13.0-beta-1.html#es7-property-initializers

ES7+ Property Initializers

Wait, assigning to properties seems like a very imperative way of defining classes! You're right, however, we designed it this way because it's idiomatic. We fully expect a more declarative syntax for property initialization to arrive in future version of JavaScript. It might look something like this:

// Future Version 
export class Counter extends React.Component { 
    static propTypes = { 
        initialCount: React.PropTypes.number
    }; 

    static defaultProps = {
        initialCount: 0
    }; 

    state = {
        count: this.props.initialCount
    }; 

    tick() {
        this.setState({
            count: this.state.count + 1 
        });
    }

    render() {
        return ( <div onClick={this.tick.bind(this)}> Clicks: { this.state.count } </div> ); 
    } 
}

Also discussed here:

6to5/6to5#619

Are there any plans to specify this? Are there any proposals being worked on to specify syntax similar to this or alternative syntax addressing this?

# Jeff Morrison (10 years ago)

I am working on a proposal that I intend to bring to the next TC39 meeting. The (very much in need of updating) gist for my proposal is here: gist.github.com/jeffmo/054df782c05639da2adb

(Note that this gist is from July of last year, which was before we eliminated @@create and imposed the new super() allocation semantics for derived classes -- so it needs to be updated to reflect those two changes. Hopefully I can find some time to update it in the next week or two)