Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the @ used for in JavaScript?

I'm reading through the MobX docs and I'm confused by the following code:

class Todo {
    id = Math.random();
    @observable title = "";
    @observable finished = false;
}

@observer
class TodoListView extends Component {
    render() {
        return <div>
            <ul>
                {this.props.todoList.todos.map(todo =>
                    <TodoView todo={todo} key={todo.id} />
                )}
            </ul>
            Tasks left: {this.props.todoList.unfinishedTodoCount}
        </div>
    }
}

What is the significance of the @ symbol?

like image 648
nomad Avatar asked May 19 '16 22:05

nomad


1 Answers

It's called a decorator, you can read all about it here:

https://github.com/wycats/javascript-decorators

A decorator is:

  • an expression that evaluates to a function that takes the target, name, and decorator descriptor as arguments and optionally returns a decorator descriptor to install on the target object
like image 79
Joe Thomas Avatar answered Nov 02 '22 08:11

Joe Thomas