当前位置: 首页 > news >正文

php网站建设制作一个空间2个网站

php网站建设制作,一个空间2个网站,物联网平台开源,网络购物网站备案在工作中我们经常遇到有多个if的判读函数#xff0c;这是一件很正常的事情#xff0c;如下#xff1a; let order function (orderType, isPay, count) {if (orderType 1) {// 充值 500if (isPay true) {// 充值成功console.log(中奖100元)} else {if (count 0) {c…        在工作中我们经常遇到有多个if的判读函数这是一件很正常的事情如下 let order function (orderType, isPay, count) {if (orderType 1) {// 充值 500if (isPay true) {// 充值成功console.log(中奖100元)} else {if (count 0) {console.log(抽到10元优惠卷)} else {console.log(请再接再励)}}} else if (orderType 2) {// 充值 200if (isPay true) {// 充值成功console.log(中奖20元)} else {if (count 0) {console.log(抽到10元优惠卷)} else {console.log(请再接再励)}}} else if (orderType 3) {// 不充钱if (count 0) {console.log(抽到10元优惠卷)} else {console.log(请再接再励)}} } 但是在很多的时候里面的if的判断会包含很多方法和数据的处理使一个函数经常会达到200行以上那么有没有优化的方法呢我们今天来看一看。 一、定义的策略 言简意赅的说就是将每一个情况都第一个个方法在需要的时候传入参数判断即可。 const strategy {order500: function (isPay, count) {if (isPay true) {// 充值成功console.log(中奖100元)} else {this.orderNoraml(isPay, count)}},order200: function (isPay, count) {// 充值 200if (isPay true) {// 充值成功console.log(中奖20元)} else {this.orderNoraml(isPay, count)}},orderNoraml: function (isPay, count) {// 不充钱if (count 0) {console.log(抽到10元优惠卷)} else {console.log(请再接再励)}}, } 然后在使用的时候传入值进行函数判断即可返回需要的参数。 function orderFunc(orderType, isPay, count) {let orderTypeObj {1: order500,2: order200,3: orderNoraml};return strategy[orderTypeObj[orderType]](isPay, count); }orderFunc(1, true, 0) orderFunc(1, false, 100) orderFunc(2, true, 0) 二、责任链模式 它的定义就是在你目前能够处理的情况进行处理如果处理不了那么就到下一个函数一直到可以处理的为止。 //原则只处理单一模式处理不了之后往后传 function order500(orderType, isPay, count) {if (orderType 1 isPay true) {// 充值成功console.log(中奖100元)} else {order200(orderType, isPay, count)} }function order200(orderType, isPay, count) {// 充值 200if (orderType 2 isPay true) {// 充值成功console.log(中奖20元)} else {orderNoraml(orderType, isPay, count)} }function orderNoraml(orderType, isPay, count) {// 不充钱if (count 0) {console.log(抽到10元优惠卷)} else {console.log(请再接再励)} } 调用方法只需要执行一个函数就行了 order500(1, true, 100) order500(1, true, 100) order500(2, true, 0) 三、函数式模式 其实我感觉应该使第二种方法的精简版使每一个方法都继承他的下一个方法然后让他们相互调用形成一个循环。 方法的定义主要体现到next下一个方法。 function order500(orderType, isPay, count) {if (orderType 1 isPay true) {// 充值成功console.log(中奖100元)} else {return next} }function order200(orderType, isPay, count) {// 充值 200if (orderType 2 isPay true) {// 充值成功console.log(中奖20元)} else {return next} }function orderNoraml(orderType, isPay, count) {// 不充钱if (count 0) {console.log(抽到10元优惠卷)} else {console.log(请再接再励)} } 定义一个class类用来继承 class chain {constructor(fn) {this.fn fnthis.next null}// 设置下一链条setNext(nextChain) {this.next nextChain}// Run 起来passRequest() {console.log(this,this)let res this.fn.apply(this,arguments)if(res next) {return this.next this.next.passRequest.apply(this.next,arguments)}return res} } 然后实现它并且相互调用。 let order500Chain new chain(order500) let order200Chain new chain(order200) let orderNoramlChain new chain(orderNoraml)order500Chain.setNext(order200Chain) order200Chain.setNext(orderNoramlChain)order500Chain.passRequest(1, false, 100) 是不是这种方法也很巧妙呢在我们工作学习的过程中大家也可以试试把他们做成一个函数式的形式一起加油吧。
http://www.dnsts.com.cn/news/251280.html

相关文章:

  • 抚顺营销型网站建设wordpress 数据库函数大全
  • 宠物网站首页模板滨州做网站的电话
  • 展示产品的网站 个人备案还是企业凡科邮箱登录
  • 运营什么网站好手机编程网站
  • 一级域名网站如何申请东莞网站关键词推广
  • 外贸网站建设浩森宇特响应式网页设计与实现论文
  • 沐雪专业网站建设越秀移动网站建设
  • 用ih5做微网站重庆网站制作机构
  • wordpress申请子站动漫制作专业名称
  • 南京专业做网站的公司哪家好海口网站开发建设
  • 域名备案网站名称jsp页面如何做网站pv统计
  • 游戏推广话术技巧黑帽seo技术培训
  • 网站建设与开发课程介绍个人博客网站怎么注册
  • 网站域名如何影响seo用墨刀做网站首页
  • 网站建设公司咨询做网站销售电销好做吗
  • dede网站qq类资源源码优化方案语文必修下册答案
  • 织梦教育网站模板南阳网站制作怎么样
  • 一般的网站建设常德网站优化公司
  • 网站侧面的虚浮代码专业公司网站建设服务公司
  • 企业网站的建设专业服务竞拍网站模板
  • 做花语的网站ueditor wordpress4.3
  • 国外做游戏评测的视频网站有哪些物流网站系统php源码
  • 招标采购平台专业的网站优化公司
  • 建设厅官方网站下载专区wordpress幻灯片简码
  • 好的设计师网站有哪些上海网站开发公司排名
  • 昆山网站建设价格企业网站推广的方式有哪些
  • 李沧建网站公司网站被k
  • 网站运营管理报告总结网站中间内容做多大尺寸的
  • 郑州网站优化怎样做庆阳北京网站建设
  • 企业进行网站建设的重要意义昆明如何做百度的网站