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

湘潭做网站 要到磐石网络河南网站优化排名

湘潭做网站 要到磐石网络,河南网站优化排名,做网站租用服务器,淄博市住房和城乡建设局官方网站桥接模式 通过桥接模式,我们可以将业务逻辑与元素的事件解耦,也可以更灵活的创建一些对象 倘若我们有如下代码 const dom document.getElementById(#test)// 鼠标移入移出事件 // 鼠标移入时改变背景色和字体颜色 dom.onmouseenter function() { th…

桥接模式

通过桥接模式,我们可以将业务逻辑与元素的事件解耦,也可以更灵活的创建一些对象

倘若我们有如下代码

const dom = document.getElementById('#test')// 鼠标移入移出事件
// 鼠标移入时改变背景色和字体颜色
dom.onmouseenter = function() { this.style.color = 'white'this.style.backgroundColor = 'black'
}// 鼠标移出时恢复背景色和字体颜色
dom.onmouseleave = function () {this.style.color = 'black'this.style.backgroundColor = 'white'
}

这里我们不难看出有部分代码是重复的,只是改变了字体颜色跟背景色,这耦合度就高起来了,我们可以是这样

const changeColor = (dom, color, val)=>{dom.style[color] = val
}
const dom = document.getElementById('#test')
dom.onmouseenter = function () {changeColor(this, 'color', 'white')changeColor(this, 'backgroundColor', 'black')
}
dom.onmouseleave = function () {changeColor(this, 'color', 'black')changeColor(this, 'backgroundColor', 'white')
}

继续优化

const changeColor = (dom, color, val)=>{dom.style[color] = val
}
const changeColorAndBgColor = (dom, color, bgColor)=>{changeColor(dom, 'color', color)changeColor(dom, 'backgroundColor', bgColor)
}
const dom = document.getElementById('#test')
dom.onmouseenter = function () {changeColorAndBgColor(this, 'white', 'black')
}
dom.onmouseleave = function () {changeColor(this, 'black', 'white')
}

多元化

在使用不同角色有相同公用的方法时可以使用这种多元化来处理

const Speed = (x, y) => {this.x = xthis.y = y
}
Speed.prototype.run = function () { console.log('first run')
}
// TODO:其他内容
const Color = color => {this.color = color
}
Color.prototype.draw = function () {console.log('first draw')
}
// TODO:其他内容
const Speak = word => {this.word = word
}
Speak.prototype.say = function () {console.log('first say')
}
// TODO:其他内容// 创建一个 球
const Ball = function (x, y, color) {this.speed = new Speed(x, y)this.color = new Color(color)
}
Ball.prototype.init = function () {this.speed.run()this.color.draw()
}// 创建一个人
const People = function (x, y, say) {this.speed = new Speed(x, y)this.speak = new Speak(say)
}
People.prototype.init = function () {this.speed.run()this.speak.say()
}
...

通过桥接灵活的创建一个对象,针对不同的对象处理不同的业务逻辑,更灵活处理差异

http://www.jinmujx.cn/news/108953.html

相关文章:

  • 有一个做5s壁纸的网站免费推广的app有哪些
  • 网站生成小程序大型网站建设公司
  • 百石网怎么做网站seo单页面优化
  • ppt 做的最好的网站全网推广推荐
  • 做网站需要的课程国家认可的赚钱软件
  • 2018年做网站微信引流推广怎么找平台
  • wordpress优化数据库变大兰州网络seo公司
  • phpcms做网站页面开发西宁网站seo
  • 网站开发PHP程序员招聘seo推广外包企业
  • 网站怎么做外链接成都关键词优化平台
  • 建设网站需要分析什么变现流量推广app
  • 中国建设银行贵州分行网站资源网站快速优化排名
  • 专业做二手房的网站有哪些网站收录教程
  • 宿州网站建设价格seo是什么职业
  • 重庆哪家做网站中小企业管理培训班
  • 90平方装修全包价格排名优化seo公司
  • 哪个网站有做兼职的贵州萝岗seo整站优化
  • 网站降权怎么做搜索引擎优化的内容有哪些
  • 网站建设公司潍坊链接式友谊
  • 网站设为主页功能怎么做如何去推广自己的产品
  • 英文外贸网站免费网站流量统计工具
  • 以下哪些是网络营销的特点前端seo优化
  • 网站微信建设鼓楼网页seo搜索引擎优化
  • 偃师制作网站谷歌推广效果怎么样
  • 嘉兴网站制作网页精准营销策略都有哪些
  • 网站开发流程说明百度竞价优化软件
  • wordpress 显示用户昵称优化公司结构
  • 重庆网站推广公司哪家好上海做关键词推广企业
  • 法院网站建设调研百度推广找谁做
  • wordpress检查全站链接百度网站官网