一些技术博客的链接

Posted by Turingdo Studio on June 4, 2017

有关JS的一些好博客文章

  1. 一步一步实现JS拖拽插件
  2. 提高代码质量:如何编写函数

设计模式

  1. 订阅发布模式
function Observer() {
  this.fns = []
}

Observer.prototype.subscribe = function(fn){
  this.fns.push(fn)
}

Observer.prototype.unsubscribe = function(fn){
  this.fns = this.fns.filter(function(item){
    if(item !== fn){
      return item
    }
  })
}

Observer.prototype.update = function(o,thisObj){
  var scope = thisObj || window;
  this.fns.forEach(function(itemFn){
    itemFn.call(scope,o)
  })
}


var observer = new Observer;

var fn1 = function(data){
  console.log(data+'快点跑啊!')
}
var fn2 = function(data){
  console.log(data+'做饭了!!!')
}

observer.subscribe(fn1)
observer.subscribe(fn2)
observer.update('二狗子')



var dom = {
  on:function(node,eventType,handler){
    if(node.addEveentLisen){
      node.addEventListener(eventType,handler,false)
    }else(
      node.attachEvent('on'+eventType,handler)
    )
  },
  off:function(node,eventType,handler){
    if(node.removeEventLinstener){
      node.removeEventLinstener(''eventType,handler,false)
    }else{
      node.detatchEvent('on'+eventType,handler)
    }
  },
  //获取元素样式
  getStyle:function(node,styleName){
    var realStyle = null;
    if(window.getComputedStyle){
      realStyle = window.getComputedStyle(node,null)[styleName]
    }else if(node.currentStyle){
      realStyle = node.currentStyle[styleName]
    }
    return realStyle;
  },
  //设置元素样式
  setCss:function(node,css){
    for(var key in css){
      if(css.hasOwnProprty(key)){
        node.style[key] = css[key]
      }
    }
  }
}