修饰器(Decorator)是一个函数,用来修改类的行为。npm
ES6 引入了这项功能,目前 Babel 转码器已经支持Decoratorbabel
首先,安装babel-core
和babel-plugin-transform-decorators
。因为后者包括在babel-preset-stage-0
之中,因此改成安装babel-preset-stage-0
亦可app
$ npm install babel-core babel-plugin-transform-decorators
而后,设置配置文件.babelrc
函数
{ "plugins": ["transform-decorators"] }
这时,Babel就能够对Decorator转码了工具
脚本中打开的命令以下测试
babel.transform("code", {plugins: ["transform-decorators"]})
下面代码中,@testable
就是一个修饰器。它修改了MyTestableClass
这个类的行为,为它加上了静态属性isTestable
ui
@testable class MyTestableClass { // ...
} function testable(target) { target.isTestable = true; } MyTestableClass.isTestable // true
基本上,修饰器的行为就是下面这样this
@decorator class A {} // 等同于
class A {} A = decorator(A) || A;
修饰器对类的行为的改变,是代码编译时发生的,而不是在运行时。这意味着,修饰器能在编译阶段运行代码,也就是说,修饰器本质就是编译时执行的函数lua
一、参数spa
修饰器函数的第一个参数,是所要修饰的目标类
function testable(target) { // ...
}
若是以为一个参数不够用,能够在修饰器外面再封装一层函数
function testable(isTestable) { return function(target) { target.isTestable = isTestable; } } @testable(true) class MyTestableClass {} MyTestableClass.isTestable // true
@testable(false) class MyClass {} MyClass.isTestable // false
上面代码中,修饰器testable
能够接受参数,这就等于能够修改修饰器的行为。
前面的例子是为类添加一个静态属性,若是想添加实例属性,能够经过目标类的prototype
对象操做
function testable(target) { target.prototype.isTestable = true; } @testable class MyTestableClass {} let obj = new MyTestableClass(); obj.isTestable // true
修饰器不只能够修饰类,还能够修饰类的属性
class Person { @readonly name() { return `${this.first} ${this.last}` } }
上面代码中,修饰器readonly
用来修饰“类”的name
方法
一、参数
此时,修饰器函数一共能够接受三个参数,第一个参数是所要修饰的目标对象,第二个参数是所要修饰的属性名,第三个参数是该属性的描述对象
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);
上面代码说明,修饰器(readonly)会修改属性的描述对象(descriptor),而后被修改的描述对象再用来定义属性。
下面是另外一个例子,修改属性描述对象的enumerable
属性,使得该属性不可遍历
class Person { @nonenumerable get kidCount() { return this.children.length; } } function nonenumerable(target, name, descriptor) { descriptor.enumerable = false; return descriptor; }
二、日志应用
下面的@log
修饰器,能够起到输出日志的做用
class Math { @log add(a, b) { return a + b; } } function log(target, name, descriptor) { var oldValue = descriptor.value; descriptor.value = function() { console.log(`Calling "${name}" with`, arguments); return oldValue.apply(null, arguments); }; return descriptor; } const math = new Math(); // passed parameters should get logged now
math.add(2, 4);
上面代码中,@log
修饰器的做用就是在执行原始的操做以前,执行一次console.log
,从而达到输出日志的目的。
修饰器有注释的做用
@testable class 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
上面代码中,外层修饰器@dec(1)
先进入,可是内层修饰器@dec(2)
先执行
除了注释,修饰器还能用来类型检查。因此,对于类来讲,这项功能至关有用。从长期来看,它将是JS代码静态分析的重要工具
修饰器只能用于类和类的方法,不能用于函数,由于存在函数提高
var counter = 0; var add = function () { counter++; }; @add function foo() { }
上面的代码,意图是执行后counter
等于1,可是实际上结果是counter
等于0。由于函数提高,使得实际执行的代码是下面这样
@add function foo() { } var counter; var add; counter = 0; add = function () { counter++; };
下面是另外一个例子
var readOnly = require("some-decorator"); @readOnly function foo() { }
上面代码也有问题,由于实际执行是下面这样
var readOnly; @readOnly function foo() { } readOnly = require("some-decorator");
总之,因为存在函数提高,使得修饰器不能用于函数。类是不会提高的,因此就没有这方面的问题。
另外一方面,若是必定要修饰函数,能够采用高阶函数的形式直接执行
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);