
结构型设计模式
- 外观型设计模式
function addEvent(dom, type, fn) {
if (dom.addEventListener) {
dom.addEventListener(type, fn, false)
} else if (dom.attachEvent) {
dom.attachEvent('on' + type, fn)
} else {
dom['on' + type] = fn
}
}
function getEvent(event) {
return event || window.event
}
function getTarget(event) {
var event = getEvent(event)
return event.target || event.srcElement
}
function preventDefault(event) {
var event = getEvent(event)
if (event.preventDefault) {
event.preventDefault()
} else {
event.returnValue = false
}
}
- 适配器模式
var A = A || {}
A.g = function (id) {
return document.getElementById(id)
}
A.on = function(id, type, fn) {
var dom = typeof id === 'string' ? this.g(id) : id
if (dom.addEventListener) {
dom.addEventListener(type, fn, false)
} else if (dom.attachEvent) {
dom.attachEvent('on' + type, fn)
} else {
dom['on' + type] = fn
}
}
// 参数适配器
function doSomething(name, title, age, color, size, prize)
// vs
/**
* obj.name: name
* obj.title: title
* obj.age: age
* obj.color: color
* obj.size: size
* obj.prize: prize
*/
function doSomething(obj)
var arr = ['JS', 'book', 2020.8.3]
var obj = {
name: '',
type: '',
time: '',
}
function arrayToObject(arr) {
return {
name: arr[0],
type: arr[1],
time: arr[2],,
}
}
- 代理模式
var Count = (function() {
var _img = new Image()
return function(params) {
var str = 'http://www.***.com/a.gif?'
for (var i in params) {
str += i '=' params[i]
}
_img.src = str
}
})()
Count({ num: 10 })
- 装饰者模式
function decorator(input, fn) {
var input = document.getElementById(input)
if (typeof input.onclick === 'function) {
var oldClickFunc = inputx.onclick
input.onclick = {
oldClickFunc()
fn()
}
} else {
input.onclick = fn
}
}
- 桥接模式
var spans = document.getElementByTagName('span')
spans[0].onmouseover = function() {
this.style.color = 'red'
this.style.background = '#ddd'
}
spans[0].onmouseout = function() {
this.style.color = '#333'
this.style.background = '#f5f5f5'
}
spans[1].onmouseover = function() {
this.getElementByTagName('strong')[0].style.color = 'red'
this.getElementByTagName('strong')[0].style.background = '#ddd'
}
spans[1].onmouseout = function() {
this.getElementByTagName('strong')[0].style.color = '#333'
this.getElementByTagName('strong')[0].style.background = '#f5f5f5'
}
function changeColor(dom, color, bg) {
dom.style.color = color
dom.style.background = bg
}
var spans = document.getElementByTagName('span')
spans[0].onmouseover = function() {
changeColor(this, 'red', '#ddd')
}
spans[0].onmouseout = function() {
changeColor(this, '#333', '#f5f5f5')
}
spans[1].onmouseover = function() {
changeColor(this.getElementByTagName('strong')[0], 'red', '#ddd')
}
spans[1].onmouseout = function() {2
changeColor(this.getElementByTagName('strong')[0], '#333', '#f5f5f5')
}
function Speed(x, y) {
this.x = x
this.y = y
}
Speed.prototype.run = function() {
console.log('run')
}
function Color(cl) {
this.color = cl
}
Color.prototype.draw = function() {
console.log('draw')
}
function Shape(sp) {
this.shape = sp
}
Shape.prototype.change = function() {
console.log('changeShape')
}
function Speek(wd) {
this.word = wd
}
Speek.prototype.say = function() {
console.log('speek')
}
function Ball(x, y, c) {
this.speed = new Speed(x, y)
this.color = new Color(c)
}
Ball.prototype.init = function() {
this.speed.run()
this.color.draw()
}
function People(x, y, f) {
this.speed = new Speed(x, y)
this.speek = new Speek(f)
}
People.prototype.init = function() {
this.speed.run()
this.speek.say()
}
var p = new People(10, 12, 16)
p.init()
- 组合模式