在 ES6 javascript 中 at 符号 (@) 有什么作用?(ECMAScript 2015) [英] What does the at symbol (@) do in ES6 javascript? (ECMAScript 2015)
问题描述
我正在查看一些 ES6 代码,但我不明白 @ 符号放在变量前面时的作用.我能找到的最接近的东西与私有字段有关?
I'm looking at some ES6 code and I don't understand what the @ symbol does when it is placed in front of a variable. The closest thing I could find has something to do with private fields?
我从 redux 库 中查看的代码:
Code I was looking at from the redux library:
import React, { Component } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'redux/react';
import Counter from '../components/Counter';
import * as CounterActions from '../actions/CounterActions';
@connect(state => ({
counter: state.counter
}))
export default class CounterApp extends Component {
render() {
const { counter, dispatch } = this.props;
return (
<Counter counter={counter}
{...bindActionCreators(CounterActions, dispatch)} />
);
}
}
这是我在该主题上找到的一篇博客文章:https://github.com/zenparsing/es-private-字段
Here is a blog post I found on the topic: https://github.com/zenparsing/es-private-fields
在这篇博文中,所有示例都在类的上下文中 - 当符号在模块中使用时,这意味着什么?
In this blog post all the examples are in the context of a class - what does it mean when the symbol is used within a module?
推荐答案
这是一个装饰器.这是一个要添加到 ECMAScript 的建议.有多个 ES6 和 ES5 等效示例:javascript-decorators.
It's a decorator. It's a proposal to be added to ECMAScript. There are multiple ES6 and ES5 equivalent examples on: javascript-decorators.
装饰器动态地改变函数、方法或类的功能,而无需直接使用子类或更改被装饰函数的源代码.
Decorators dynamically alter the functionality of a function, method, or class without having to directly use subclasses or change the source code of the function being decorated.
它们通常用于控制访问、注册、注释.
They are commonly used to control access, registration, annotation.
这篇关于在 ES6 javascript 中 at 符号 (@) 有什么作用?(ECMAScript 2015)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!