decorator是ES7引入的功能,它是一個(gè)函數(shù),用來(lái)修改類甚至于是方法的行為。
類的修飾
一個(gè)簡(jiǎn)單的栗子:
@testable
class MyTestableClass {
? // ...
}
function testable(target) {
? ????target.isTestable = true;
}
MyTestableClass.isTestable // true
上面的例子中,@testable就是一個(gè)修飾器,它修改了它下面的這個(gè)MyTestableClass類的行為:給它加上了一個(gè)靜態(tài)屬性isTestable。
注意,修飾器對(duì)類的行為的改變,是在代碼編譯的時(shí)候就發(fā)生的,而不是在代碼運(yùn)行時(shí)。這就意味著,修飾器能夠在編譯階段運(yùn)行代碼。也就是說(shuō),修飾器本質(zhì)就是編譯時(shí)執(zhí)行的函數(shù)。
如果覺(jué)得一個(gè)參數(shù)不夠用,可以在修飾器外面再封裝一層函數(shù)
eg:
function testable(isTestable) {
? return function(target) {
? ? target.isTestable = isTestable;
? }
}
@testable(true)
class MyTestableClass { }
MyTestableClass.isTestable // true
@testable(false)
class MyClass {}
MyClass.isTestable // false
在這個(gè)例子中,修飾器可以接受參數(shù),以控制修飾器的行為。
前面的例子都是為類添加一個(gè)靜態(tài)屬性,當(dāng)然,我們也可以通過(guò)為目標(biāo)類的prototype添加屬性來(lái)添加實(shí)例屬性。
function testable(target) {
? target.prototype.isTestable = true;
}
@testable
class MyTestableClass {}
let obj = new MyTestableClass();
obj.isTestable // true
這樣通過(guò)prototype添加的屬性,就可以在實(shí)例上調(diào)用。
下面是另一個(gè)例子,在這個(gè)栗子中,包含export和import代碼,并且還加入了閉包的思想。
// mixins.js
export function mixins(...list) {
? return function (target) {
? ? Object.assign(target.prototype, ...list)
? }
}
// main.js
import { mixins } from './mixins'
const Foo = {
? foo() { console.log('foo') }
};
@mixins(Foo)
class MyClass {}
let obj = new MyClass();
obj.foo() // 'foo'
這就是上一篇文檔中的mixin:將Foo類的方法添加到了MyClass的實(shí)例上面。
方法的修飾
修飾器不僅可以修飾類,還可以修飾類的屬性。
例子:
class Person {
? @readonly
? name() { return `${this.first} ${this.last}` }
}
此時(shí),修飾器函數(shù)一共可以接受三個(gè)參數(shù):第一個(gè)參數(shù)是要修飾的目標(biāo)對(duì)象,第二個(gè)參數(shù)是要修飾的屬性名,第三個(gè)參數(shù)是該屬性的描述對(duì)象。
function readonly(target, name, descriptor){
? // descriptor對(duì)象原來(lái)的值如下
? // {
????? //? value: specifiedFunction,
? ????//? enumerable: false,
????? //? configurable: true,
? ????//? writable: true
? // };
? descriptor.writable = false;
? return descriptor;
}
修飾器會(huì)修改屬性的描述對(duì)象,然后被修改的描述對(duì)象再用來(lái)定義屬性。
這是另一個(gè)例子:
class Person {
? @nonenumerable
? get kidCount() { return this.children.length; }
}
function nonenumerable(target, name, descriptor) {
? descriptor.enumerable = false;
? return descriptor;
}
修飾器還有注釋的作用:
@testable
class Person {
? @readonly
? @nonenumerable
? name() { return `${this.first} ${this.last}` }
}
我們可以直接從修飾器的名稱看出,Person類是可測(cè)試的,而name方法是只讀和不可枚舉的。
如果同一個(gè)方法有多個(gè)修飾器,會(huì)先從外到內(nèi)進(jìn)入,然后從內(nèi)向外執(zhí)行。
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
修飾器不能用于函數(shù)
修飾器只能用于“類”和類的方法,不能用于函數(shù),因?yàn)榇嬖诤瘮?shù)提升。
如果真的需要修飾函數(shù),可以采用高階函數(shù)的形式直接 執(zhí)行。
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是一個(gè)第三方模塊,提供了幾個(gè)常見(jiàn)的修飾器,通過(guò)它可以更好地理解修飾器。
Mixin
利用修飾器,可以實(shí)現(xiàn)Mixin模式。
Mixin模式,就是對(duì)象繼承的一種替代方案,中文譯為“混入”(mix in),意為在一個(gè)對(duì)象之中混入另外一個(gè)對(duì)象的方法。
例子:
const Foo = {
? foo() { console.log('foo') }
};
class MyClass {}
Object.assign(MyClass.prototype, Foo);
let obj = new MyClass();
obj.foo() // 'foo'
這是一個(gè)混入的簡(jiǎn)單實(shí)現(xiàn),下面我們用修飾器來(lái)實(shí)現(xiàn)它:
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"
---------------------
原文轉(zhuǎn)自:https://blog.csdn.net/u014328357/article/details/7360679
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請(qǐng)附上博文鏈接!