yield next和yield* next的区别
yield next和yield* next之間到底有什么區(qū)別?為什么需要yield* next?經(jīng)常會(huì)有人提出這個(gè)問題。雖然我們?cè)诖a中會(huì)盡量避免使用yield* next以減少新用戶的疑惑,但還是經(jīng)常會(huì)有人問到這個(gè)問題。為了體現(xiàn)自由,我們?cè)趉oa框架內(nèi)部使用了yield* next,但是為了避免引起混亂我們并不提倡這樣做。
相關(guān)文檔,可以查看這里的說明harmony proposal.
yield委托(delegating)做了什么?
假設(shè)有下面兩個(gè)generator函數(shù):
function* outer() {yield 'open'yield inner()yield 'close' }function* inner() {yield 'hello!' }通過調(diào)用函數(shù)outer()能產(chǎn)出哪些值呢?
var gen = outer() gen.next() // -> 'open' gen.next() // -> a generator gen.next() // -> 'close'但如果我們把其中的yield inner()改成yield* inner(),結(jié)果又會(huì)是什么呢?
var gen = outer() gen.next() // -> 'open' gen.next() // -> 'hello!' gen.next() // -> 'close'事實(shí)上,下面兩個(gè)function本質(zhì)上來說是等價(jià)的:
function* outer() {yield 'open'yield* inner()yield 'close' }function* outer() {yield 'open'yield 'hello!'yield 'close' }從這個(gè)意義上來說,委托的generator函數(shù)替代了yield*關(guān)鍵字的作用!
這與Co或Koa有什么關(guān)系呢?
Generator函數(shù)已經(jīng)很讓人抓狂了,它并不能幫助Koa的generator函數(shù)使用Co來控制流程。很多人都會(huì)被本地的generator函數(shù)和Co框架提供的功能搞暈。
假設(shè)有以下generator函數(shù):
function* outer() {this.body = yield inner }function* inner() {yield setImmediatereturn 1 }如果使用Co,它實(shí)際上等價(jià)于下面的代碼:
function* outer() {this.body = yield co(function inner() {yield setImmediatereturn 1}) }但是如果我們使用yield委托,完全可以去掉Co的調(diào)用:
function* outer() {this.body = yield* inner() }那么最終執(zhí)行的代碼會(huì)變成下面這樣:
function* outer() {yield setImmediatethis.body = 1 }每一個(gè)Co的調(diào)用都是一個(gè)閉包,因此它會(huì)或多或少地存在一些性能上的開銷。不過你也不用太擔(dān)心,這個(gè)開銷不會(huì)很大,但是如果使用委托yield,我們就可以降低對(duì)第三方庫(kù)的依賴而從代碼級(jí)別避免這種開銷。
有多快?
這里有一個(gè)鏈接,是之前我們討論過的有關(guān)該話題的內(nèi)容:https://github.com/koajs/compose/issues/2. 你不會(huì)看到有太多的性能差異(至少在我們看來),特別是因?yàn)閷?shí)際項(xiàng)目中的代碼會(huì)顯著地降低這些基準(zhǔn)。因此,我們并不提倡使用yield* next,不過在內(nèi)部使用也并沒有壞處。
有趣的是,通過使用yield* next,Koa比Express要快!Koa沒有使用dispatcher(調(diào)度程序),這與Express不同。Express使用了許多的調(diào)度程序,如connect, router等。
使用委托yield,Koa事實(shí)上將:
co(function* () {var start = Date.getTime()this.set('X-Powered-By', 'koa')if (this.path === '/204')this.status = 204if (!this.status) {this.status = 404this.body = 'Page Not Found'}this.set('X-Response-Time', Date.getTime() - start) }).call(new Context(req, res))拆解成:
app.use(function* responseTime(next) {var start = Date.getTime()yield* nextthis.set('X-Response-Time', Date.getTime() - start) })app.use(function* poweredBy(next) {this.set('X-Powered-By', 'koa')yield* next })app.use(function* pageNotFound(next) {yield* nextif (!this.status) {this.status = 404this.body = 'Page Not Found'} })app.use(function* (next) {if (this.path === '/204')this.status = 204 })一個(gè)理想的Web application看起來就和上面這段代碼差不多。在上面使用Co的那段代碼中,唯一的開銷就是啟動(dòng)單個(gè)co實(shí)例和我們自己的Context構(gòu)造函數(shù),方便起見,我們將node的req和res對(duì)象封裝進(jìn)去了。
使用它進(jìn)行類型檢查
如果將yield*應(yīng)用到不是generator的函數(shù)上,你將會(huì)看到下面的錯(cuò)誤:
TypeError: Object function noop(done) {done(); } has no method 'next'這是因?yàn)榛旧先魏尉哂衝ext方法的東西都被認(rèn)為是一個(gè)generator函數(shù)!就我個(gè)人而言,我很喜歡這個(gè),因?yàn)槟J(rèn)我會(huì)假設(shè)我就是一個(gè)yield* gen()。我重寫了很多我的代碼來使用generator函數(shù),如果我看到某些東西沒有被寫成generator函數(shù),那么我會(huì)想,我可以將它們轉(zhuǎn)換成generator函數(shù)而使代碼更簡(jiǎn)化嗎?
當(dāng)然,這也許并不適用于所有人,你也許能找到其它你想要進(jìn)行類型檢查的原因。
上下文
Co會(huì)在相同的上下文中調(diào)用所有連續(xù)的可yield的代碼。如果你想在yield function中改變調(diào)用的上下文,會(huì)有些麻煩。看下面的代碼:
function Thing() {this.name = 'thing' }Thing.prototype.print = function (done) {var self = thissetImmediate(function () {console.log(self.name)}) }// in koa app.use(function* () {var thing = new Thing()this.body = yield thing.print })這里你會(huì)發(fā)現(xiàn)this.body是undefined,這是因?yàn)镃o事實(shí)上做了下面的事情:
app.use(function* () {var thing = new Thing()this.body = yield function (done) {thing.print.call(this, done)} })而這里的this指向的是Koa的上下文,而不是thing。
上下文在JavaScript中很重要,在使用yield*時(shí),可以考慮使用下面兩種方法之一:
yield* context.generatorFunction() yield context.function.bind(context)這樣可以讓你避開99%的generator函數(shù)的上下文問題,從而避免了使用yield context.method給你帶來的困擾!
本文轉(zhuǎn)自Jaxu博客園博客,原文鏈接:http://www.cnblogs.com/jaxu/p/6344366.html,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的yield next和yield* next的区别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: brk(), sbrk() 用法详解【转
- 下一篇: 跟我学交换机配置(一)