JavaScript中的@有什么用?

What is the @ used for in JavaScript?

我正在通读 MobX docs,但我对以下代码感到困惑:

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>
    }
}

@符号有什么意义?

它被称为装饰器,您可以在这里阅读所有相关信息:

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