当前位置:K88软件开发文章中心编程语言JavaScriptECMAScript → 文章内容

ECMAScript 6 修饰器(Decorator)

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 15:38:42

由 youj 创建, 最后一次修改 2016-09-20 类的修饰方法的修饰为什么修饰器不能用于函数?core-decorators.js使用修饰器实现自动发布事件MixinTraitBabel转码器的支持类的修饰修饰器(Decorator)是一个函数,用来修改类的行为。这是ES7的一个提案,目前Babel转码器已经支持。修饰器对类的行为的改变,是代码编译时发生的,而不是在运行时。这意味着,修饰器能在编译阶段运行代码。function testable(target) { target.isTestable = true;}@testableclass MyTestableClass {}console.log(MyTestableClass.isTestable) // true上面代码中,@testable就是一个修饰器。它修改了MyTestableClass这个类的行为,为它加上了静态属性isTestable。基本上,修饰器的行为就是下面这样。@decoratorclass A {}// 等同于class A {}A = decorator(A) || A;也就是说,修饰器本质就是编译时执行的函数。修饰器函数的第一个参数,就是所要修饰的目标类。function testable(target) { // ...}上面代码中,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;}@testableclass MyTestableClass {}let obj = new MyTestableClass();obj.isTestable // true上面代码中,修饰器函数testable是在目标类的prototype对象上添加属性,因此就可以在实例上调用。下面是另外一个例子。// 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'上面代码通过修饰器mixins,把Foo类的方法添加到了MyClass的实例上面。可以用Object.assign()模拟这个功能。const Foo = { foo() { console.log('foo') }};class MyClass {}Object.assign(MyClass.prototype, Foo);let obj = new MyClass();obj.foo() // 'foo'方法的修饰修饰器不仅可以修饰类,还可以修饰类的属性。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 nowmath.add(2, 4);上面代码中,@log修饰器的作用就是在执行原始的操作之前,执行一次console.log,从而达到输出日志的目的。修饰器有注释的作用。@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上面代码中,外层修饰器@dec(1)先进入,但是内层修饰器@dec(2)先执行。除了注释,修饰器还能用来类型检查。所以,对于类来说,这项功能相当有用。从长期来看,它将是JavaScript代码静态分析的重要工具。为什么修饰器不能用于函数?修饰器只能用于类和类的方法,不能用于函数,因为存在函数提升。var counter = 0;var add = function () { counter++;};@addfunction foo() {}上面的代码,意图是执行后counter等于1,但是实际上结果是counter等于0。因为函数提升,使得实际执行的代码是下面这样。var counter;var add;@addfunction foo() {}counter = 0;add = function () { counter++;};下面是另一个例子。var readOnly = require("some-decorator");@readOnlyfunction foo() {}上面代码也有问题,因为实际执行是下面这样。var readOnly;@readOnlyfunction foo() {}readOnly = require("some-decorator");总之,由于存在函数提升,使得修饰器不能用于函数。类是不会提升的,所以就没有这方面的问题。core-decorators.jscore-decorators.js

[1] [2] [3]  下一页


ECMAScript 6 修饰器(Decorator)