decorator是ES7引入的功能,它是一個(gè)函數(shù)祈坠,用來修改類甚至于是方法的行為瓣戚。
類的修飾
一個(gè)簡單的栗子:
@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)行代碼雄坪。也就是說厘熟,修飾器本質(zhì)就是編譯時(shí)執(zhí)行的函數(shù)。
如果覺得一個(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)然脑蠕,我們也可以通過為目標(biāo)類的prototype添加屬性來添加實(shí)例屬性。
function testable(target) {
? target.prototype.isTestable = true;
}
@testable
class MyTestableClass {}
let obj = new MyTestableClass();
obj.isTestable // true
這樣通過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ì)象原來的值如下
? // {
????? //? value: specifiedFunction,
? ????//? enumerable: false,
????? //? configurable: true,
? ????//? writable: true
? // };
? descriptor.writable = false;
? return descriptor;
}
修飾器會(huì)修改屬性的描述對(duì)象,然后被修改的描述對(duì)象再用來定義屬性砸西。
這是另一個(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類是可測試的,而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è)常見的修飾器肋杖,通過它可以更好地理解修飾器。
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è)混入的簡單實(shí)現(xiàn),下面我們用修飾器來實(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)附上博文鏈接吩案!