ES6 -- 修饰器

来源:互联网 发布:excel如何删除重复数据 编辑:程序博客网 时间:2024/05/22 14:32

decorator是ES7引入的功能,它是一个函数,用来修改类甚至于是方法的行为。

类的修饰

一个简单的栗子:

@testableclass MyTestableClass {  // ...}function testable(target) {  target.isTestable = true;}MyTestableClass.isTestable // true

上面的栗子中,@testable就是一个修饰器,它修改了它下面的这个MyTestableClass类的行为:给它加上了一个静态属性isTestable。

注意,修饰器对类的行为的改变,是在代码编译的时候就发生的,而不是在代码运行时。
这就意味着,修饰器能够在编译阶段运行代码。也就是说,修饰器本质就是编译时执行的函数

如果觉得一个参数不够用,可以在修饰器外面再封装一层函数

例子:

function testable(isTestable) {  return function(target) {    target.isTestable = isTestable;  }}@testable(true)class MyTestableClass {}MyTestableClass.isTestable // true@testable(false)class MyClass {}MyClass.isTestable // false

在这个例子中,修饰器可以接受参数,以控制修饰器的行为。

前面的例子都是为类添加一个静态属性,当然,我们也可以通过为目标类的prototype添加属性来添加实例属性。

function testable(target) {  target.prototype.isTestable = true;}@testableclass MyTestableClass {}let obj = new MyTestableClass();obj.isTestable // true

这样通过prototype添加的属性,就可以在实例上调用。

下面是另一个例子,在这个栗子中,包含export和import代码,并且还加入了闭包的思想。

// 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'

这就是上一篇文档中的mixin:将Foo类的方法添加到了MyClass的实例上面。

方法的修饰

修饰器不仅可以修饰类,还可以修饰类的属性。

例子:

class Person {  @readonly  name() { return `${this.first} ${this.last}` }}

此时,修饰器函数一共可以接受三个参数:第一个参数是要修饰的目标对象,第二个参数是要修饰的属性名,第三个参数是该属性的描述对象。

function readonly(target, name, descriptor){  // descriptor对象原来的值如下  // {  //   value: specifiedFunction,  //   enumerable: false,  //   configurable: true,  //   writable: true  // };  descriptor.writable = false;  return descriptor;}

修饰器会修改属性的描述对象,然后被修改的描述对象再用来定义属性。

这是另一个例子:

class Person {  @nonenumerable  get kidCount() { return this.children.length; }}function nonenumerable(target, name, descriptor) {  descriptor.enumerable = false;  return descriptor;}

修饰器还有注释的作用:

@testableclass Person {  @readonly  @nonenumerable  name() { return `${this.first} ${this.last}` }}

我们可以直接从修饰器的名称看出,Person类是可测试的,而name方法是只读和不可枚举的。

如果同一个方法有多个修饰器,会先从外到内进入,然后从内向外执行。

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

修饰器不能用于函数

修饰器只能用于“类”和类的方法,不能用于函数,因为存在函数提升。

如果真的需要修饰函数,可以采用高阶函数的形式直接 执行。

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

core-decorators.js是一个第三方模块,提供了几个常见的修饰器,通过它可以更好地理解修饰器。

Mixin

利用修饰器,可以实现Mixin模式。

Mixin模式,就是对象继承的一种替代方案,中文译为“混入”(mix in),意为在一个对象之中混入另外一个对象的方法。

例子:

const Foo = {  foo() { console.log('foo') }};class MyClass {}Object.assign(MyClass.prototype, Foo);let obj = new MyClass();obj.foo() // 'foo'

这是一个混入的简单实现,下面我们用修饰器来实现它:

export function mixins(...list) {  return function (target) {    Object.assign(target.prototype, ...list);  };}
import { mixins } from './mixins';const Foo = {  foo() { console.log('foo') }};@mixins(Foo)class MyClass {}let obj = new MyClass();obj.foo() // "foo"
原创粉丝点击