Javascript 函数调用前面的@是什么意思?
链接到的项目有一个名为connectToStores的函数,该函数按如下方式导入(使用es6语法) 然而,当它被调用时(参见上面的链接),前面有一个Javascript 函数调用前面的@是什么意思?,javascript,reactjs,ecmascript-6,babeljs,Javascript,Reactjs,Ecmascript 6,Babeljs,链接到的项目有一个名为connectToStores的函数,该函数按如下方式导入(使用es6语法) 然而,当它被调用时(参见上面的链接),前面有一个@ @connectToStores([RepoStore, StargazersByRepoStore, UserStore], getState) 最初的connectToStores函数似乎是一个常规导出函数。为什么有@放在它前面 export default function connectToStores(stores, getState
@
@connectToStores([RepoStore, StargazersByRepoStore, UserStore], getState)
最初的connectToStores函数似乎是一个常规导出函数。为什么有@
放在它前面
export default function connectToStores(stores, getState) {
return function (DecoratedComponent) {
const displayName =
DecoratedComponent.displayName ||
DecoratedComponent.name ||
'Component';
return class StoreConnector extends Component {
static displayName = `connectToStores(${displayName})`;
constructor(props) {
super(props);
this.handleStoresChanged = this.handleStoresChanged.bind(this);
this.state = getState(props);
}
componentWillMount() {
stores.forEach(store =>
store.addChangeListener(this.handleStoresChanged)
);
}
componentWillReceiveProps(nextProps) {
if (!shallowEqual(nextProps, this.props)) {
this.setState(getState(nextProps));
}
}
componentWillUnmount() {
stores.forEach(store =>
store.removeChangeListener(this.handleStoresChanged)
);
}
handleStoresChanged() {
this.setState(getState(this.props));
}
render() {
return <DecoratedComponent {...this.props} {...this.state} />;
}
};
};
}
导出默认函数connectToStores(stores,getState){
返回函数(DecoratedComponent){
常量显示名=
DecoratedComponent.displayName||
DecoratedComponent.name||
“组成部分”;
返回类StoreConnector扩展组件{
静态displayName=`connectToStores(${displayName})`;
建造师(道具){
超级(道具);
this.handleStoresChanged=this.handleStoresChanged.bind(this);
this.state=getState(道具);
}
组件willmount(){
stores.forEach(store=>
store.addChangeListener(this.handleStoresChanged)
);
}
组件将接收道具(下一步){
如果(!shallewequal(nextrops,this.props)){
this.setState(getState(nextrops));
}
}
组件将卸载(){
stores.forEach(store=>
store.removeChangeListener(this.handleStoresChanged)
);
}
handleStoresChanged(){
this.setState(getState(this.props));
}
render(){
返回;
}
};
};
}
那些@
是(它们被传送通过)。根据规范:
也可以装饰一个类。在这种情况下,decorator接受目标构造函数
export default function connectToStores(stores, getState) {
return function (DecoratedComponent) {
const displayName =
DecoratedComponent.displayName ||
DecoratedComponent.name ||
'Component';
return class StoreConnector extends Component {
static displayName = `connectToStores(${displayName})`;
constructor(props) {
super(props);
this.handleStoresChanged = this.handleStoresChanged.bind(this);
this.state = getState(props);
}
componentWillMount() {
stores.forEach(store =>
store.addChangeListener(this.handleStoresChanged)
);
}
componentWillReceiveProps(nextProps) {
if (!shallowEqual(nextProps, this.props)) {
this.setState(getState(nextProps));
}
}
componentWillUnmount() {
stores.forEach(store =>
store.removeChangeListener(this.handleStoresChanged)
);
}
handleStoresChanged() {
this.setState(getState(this.props));
}
render() {
return <DecoratedComponent {...this.props} {...this.state} />;
}
};
};
}
// A simple decorator
@annotation
class MyClass { }
function annotation(target) {
// Add a property on target
target.annotated = true;
}