| 
                         例子 
- function order(options) { 
 -   return { 
 -     next: (callback) => callback(options), 
 -   } 
 - } 
 -  
 - function order500(options) { 
 -   const { orderType, pay } = options 
 -   if (orderType === 1 && pay === true) { 
 -     console.log('500 元定金预购, 得到 100 元优惠券') 
 -     return { 
 -       next: () => {}, 
 -     } 
 -   } else { 
 -     return { 
 -       next: (callback) => callback(options), 
 -     } 
 -   } 
 - } 
 -  
 - function order200(options) { 
 -   const { orderType, pay } = options 
 -   if (orderType === 2 && pay === true) { 
 -     console.log('200 元定金预购, 得到 50 元优惠券') 
 -     return { 
 -       next: () => {}, 
 -     } 
 -   } else { 
 -     return { 
 -       next: (callback) => callback(options), 
 -     } 
 -   } 
 - } 
 -  
 - function orderCommon(options) { 
 -   const { orderType, stock } = options 
 -   if (orderType === 3 && stock > 0) { 
 -     console.log('普通购买, 无优惠券') 
 -     return {} 
 -   } else { 
 -     console.log('库存不够, 无法购买') 
 -   } 
 - } 
 -  
 - order({ 
 -   orderType: 3, 
 -   pay: true, 
 -   stock: 500, 
 - }) 
 -   .next(order500) 
 -   .next(order200) 
 -   .next(orderCommon) 
 - // 打印出 “普通购买, 无优惠券” 
 
  
上面的代码,对 order 相关的进行了解耦,order500,order200、orderCommon 等都是可以单独调用的。                          (编辑:泰州站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |