詳解es6超好用的語法糖Decorator
Decorator
(修飾器/裝飾器)是es6提出的語法糖,用于修改類的行為。不過目前主流瀏覽器都沒有很好的支持,我們需要用babel來轉(zhuǎn)換為瀏覽器能識(shí)別的語言。在這篇文章中將介紹decorator
的基礎(chǔ)用法和一些應(yīng)用實(shí)例。
1.修飾類
(1) 基礎(chǔ)用法
@testable class MyClass{} function testable(target){ target.isTestable=true } console.log(MyClass.isTestable) // true
貼一下babel轉(zhuǎn)換后的代碼,
var _class; let MyClass = testable(_class = class MyClass {}) || _class; function testable(target) { target.isTestable = true; }
也可以在prototype上修改屬性,也可以為修飾器添加多個(gè)參數(shù)。
@testable(false) class MyAnotherClass{ } function testable(status){ return target=>{target.prototype.isTestable=status} } console.log('MyClass.isTestable',MyAnotherClass.prototype.isTestable) // false
當(dāng)然我們通過修飾器,把某個(gè)對(duì)象的方法添加到目標(biāo)類的實(shí)例上,注意要在類的prototype上添加。
const foo={isTestable:true} function testable(...list){ return target=>{Object.assign(target.prototype,...list)} } @testable(foo) class MyAnotherClass{} const obj=new MyAnotherClass() console.log('MyClass.isTestable',obj.isTestable) // true
(2) 應(yīng)用
在React App
的開發(fā)中,使用redux
通常需要react-redux
中的connect
方法,將兩者結(jié)合在一起。通常的寫法是:
class MyReactComponent extends React.Component {} export default connect(mapStateToProps, mapDispatchToProps)(MyReactComponent);
如果使用decorator
,代碼可讀性更高了一些。
@connect(mapStateToProps, mapDispatchToProps) export default class MyReactComponent extends React.Component {}
2.修飾方法
(1).基礎(chǔ)用法
// target:在方法中的target指向類的prototype function readonly(target,key,descriptor){ descriptor.writable=false return descriptor } class MyClass{ @readonly print(){console.log(`a:${this.a}`)} }
(2).js中Object的屬性
var person = {} Object.defineProperty(person,'name',{ configurable:false,//能否使用delete、能否需改屬性特性、或能否修改訪問器屬性、,false為不可重新定義,默認(rèn)值為true enumerable:false,//對(duì)象屬性是否可通過for-in循環(huán),flase為不可循環(huán),默認(rèn)值為true writable:false,//對(duì)象屬性是否可修改,flase為不可修改,默認(rèn)值為true value:'xiaoming' //對(duì)象屬性的默認(rèn)值,默認(rèn)值為undefined });
對(duì)應(yīng)到descriptor為下面四個(gè)屬性:
{ value: specifiedFunction, enumerable: false, configurable: true, writable: true };
(3). 應(yīng)用
我們開始寫一個(gè)@log修飾器,可以輸出日志:
class Math{ @log add(a,b){ return a+b } } const math=new Math() math.add(1,2) function log(target,name,descriptor){ const oldValue=descriptor.value descriptor.value=function(){ console.log(`calling ${name} with ${JSON.stringify(arguments)}`) return oldValue.apply(this,arguments) } return descriptor }
上面的代碼中,@log作用是在返回結(jié)果前,打印函數(shù)名和其參數(shù),起到輸出日至的作用。上面的程序運(yùn)行后,控制臺(tái)將輸出:
calling add with {"0":1,"1":2}
(4). 多個(gè)修飾器
良好命名的修飾器可以起到簡(jiǎn)潔注釋的作用,如下:
class Example { @readonly @enumable method(){} }
多個(gè)修飾器的執(zhí)行順序是由外向內(nèi)進(jìn)入;再由內(nèi)向外執(zhí)行。
class Example { @decorator(1) @decorator(2) method(){} } function decorator(id){ console.log('id is ',id) return (target,property,descriptor)=>console.log('executed',id) }
控制臺(tái)輸出
id is 1
id is 2
executed 2
executed 1
附錄:babel配置
babel插件transform-decorators
還沒有正式版,我們可以用transform-decorators-legacy
。
安裝babel
yarn add babel-plugin-transform-decorators-legacy babel-preset-es2017
配置.babelrc
{ "presets": ["es2017"], "plugins":[ "transform-decorators-legacy" ] }
執(zhí)行編譯后的文件
因?yàn)槲覀優(yōu)榱藴y(cè)試,沒必要非得放在瀏覽器里看了,可以用node執(zhí)行babel轉(zhuǎn)換后的文件。直接運(yùn)行yarn start
。
// package.json "scripts": { "build": "babel ./decorator -d lib", "start":"yarn build && node ./lib/index.js" },
參考鏈接
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js點(diǎn)擊列表文字對(duì)應(yīng)該行顯示背景顏色的實(shí)現(xiàn)代碼
這篇文章主要介紹了js點(diǎn)擊列表文字對(duì)應(yīng)該行顯示背景顏色的實(shí)現(xiàn)代碼,感興趣的小伙伴可以參考下2015-08-08用cookies實(shí)現(xiàn)的可記憶的樣式切換效果代碼下載
比較不錯(cuò)的用cookies實(shí)現(xiàn)的可記憶的樣式切換效果,這個(gè)思路也在一定程序,方便客戶的長(zhǎng)期使用。2007-12-124 種滾動(dòng)吸頂實(shí)現(xiàn)方式的比較
這篇文章主要介紹了4種滾動(dòng)吸頂實(shí)現(xiàn)方式的比較,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04比較簡(jiǎn)單的一個(gè)符合web標(biāo)準(zhǔn)的JS調(diào)用flash方法
比較簡(jiǎn)單的一個(gè)符合web標(biāo)準(zhǔn)的JS調(diào)用flash方法...2007-11-11JavaScript增加數(shù)組中指定元素的5種方法總結(jié)
在JS中數(shù)組方法是非常重要且常用的的方法,在此整理總結(jié)一番,下面這篇文章主要給大家介紹了關(guān)于JavaScript增加數(shù)組中指定元素的5種方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02bootstrap datepicker限定可選時(shí)間范圍實(shí)現(xiàn)方法
這篇文章主要介紹了bootstrap datepicker限定可選時(shí)間范圍的實(shí)現(xiàn)方法,本文涉及到相關(guān)知識(shí)點(diǎn),通過實(shí)例給大家介紹的非常詳細(xì),需要的朋友可以參考下2016-09-09