ES6之装饰器

date
Jun 13, 2018
slug
aqwvyyiw
status
Published
tags
JavaScript
summary
type
Post
 
[toc]

类的装饰

类装饰器是一个对类进行处理的函数。装饰器函数的第一个参数,就是所要装饰的目标类。如果觉得一个参数不够用,可以在装饰器外面再封装一层函数。
function testable(isTestable) {  return function(target) {    target.isTestable = isTestable;  }}@testable(true)class MyTestableClass {}MyTestableClass.isTestable // true
注意,装饰器对类的行为的改变,是代码编译时发生的,而不是在运行时。这意味着,装饰器能在编译阶段运行代码。也就是说,装饰器本质就是编译时执行的函数。
前面的例子是为类添加一个静态属性,如果想添加实例属性,可以通过目标类的 prototype 对象操作。
function testable(target) {  target.prototype.isTestable = true;}@testableclass MyTestableClass {}let obj = new MyTestableClass();obj.isTestable // true
下面是另外一个例子。
// mixins.jsexport function mixins(...list) {  return function (target) {    Object.assign(target.prototype, ...list)  }}// main.jsimport { mixins } from './mixins'const Foo = {  foo() { console.log('foo') }};@mixins(Foo)class MyClass {}let obj = new MyClass();obj.foo() // 'foo'
可以用 Object.assign()模拟这个功能。
const Foo = {  foo() { console.log('foo') }};class MyClass {}Object.assign(MyClass.prototype, Foo);let obj = new MyClass();obj.foo() // 'foo'
实际开发中,React 与 Redux 库结合使用时,常常需要写成下面这样。
class MyReactComponent extends React.Component {}export default connect(mapStateToProps, mapDispatchToProps)(MyReactComponent);// 等效于@connect(mapStateToProps, mapDispatchToProps)export default class MyReactComponent extends React.Component {}

方法的装饰

装饰器不仅可以装饰类,还可以装饰类的属性。装饰器函数一共可以接受三个参数。
function readonly(target, name, descriptor){  // descriptor对象原来的值如下  // {  //   value: specifiedFunction,  //   enumerable: false,  //   configurable: true,  //   writable: true  // };  descriptor.writable = false;  return descriptor;}readonly(Person.prototype, 'name', descriptor);// 类似于Object.defineProperty(Person.prototype, 'name', descriptor);class Person {  @readonly  name() { return `${this.first} ${this.last}` }}
装饰器第一个参数是类的原型对象,上例是 Person.prototype,装饰器的本意是要“装饰”类的实例,但是这个时候实例还没生成,所以只能去装饰原型(这不同于类的装饰,那种情况时 target 参数指的是类本身)。第二个参数是所要装饰的属性名,第三个参数是该属性的描述对象。
装饰器有注释的作用,即通过装饰器名字来提高类或函数的语义化。下面是使用 Decorator 写法的组件,看上去一目了然。
@Component({  tag: 'my-component',  styleUrl: 'my-component.scss'})export class MyComponent {  @Prop() first: string;  @Prop() last: string;  @State() isVisible: boolean = true;  render() {    return (      <p>Hello, my name is {this.first} {this.last}</p>    );  }}
如果同一个方法有多个装饰器,会像剥洋葱一样,先从外到内进入,然后由内向外执行。
function dec(id){  console.log('evaluated', id);  return (target, property, descriptor) => console.log('executed', id);}class Example {    @dec(1)    @dec(2)    method(){}}// evaluated 1// evaluated 2// executed 2// executed 1
除了注释,装饰器还能用来类型检查。所以,对于类来说,这项功能相当有用。从长期来看,它将是 JavaScript 代码静态分析的重要工具。

为什么装饰器不能用于函数?

装饰器只能用于类和类的方法,不能用于函数,因为存在函数提升。类是不会提升的。如果一定要装饰函数,可以采用高阶函数的形式直接执行。
function doSomething(name) {  console.log('Hello, ' + name);}function loggingDecorator(wrapped) {  return function() {    console.log('Starting');    const result = wrapped.apply(this, arguments);    console.log('Finished');    return result;  }}const wrapped = loggingDecorator(doSomething);

第三方库

core-decorators.js是一个第三方模块,提供了几个常见的装饰器,通过它可以更好地理解装饰器。 traits-decorator提供的 traits 装饰器,不仅可以接受对象,还可以接受 ES6 类作为参数。

Mixin

在装饰器的基础上,可以实现 Mixin 模式。所谓 Mixin 模式,就是对象继承的一种替代方案,中文译为“混入”(mix in),意为在一个对象之中混入另外一个对象的方法。

© 刘德华 2020 - 2023