jsonp和事件发布监听

模拟jsonp

 var id = 0;
function JSONP(url,param,cb){
var callbackName = "json_" + id++;
var arg = Array.prototype.slice.call(arguments);
var _cb = arg.pop();
window[callbackName] = function(data){
head.removeChild(scriptNode);
window[callbackName] = null;
if(typeof _cb == "function"){
_cb(data);
}
}
var _url = url.replace("callback=?","callback="+callbackName);
if(param){
var arr = [];
for(i in param){
arr.push(i+"="+arr[i]);
}
_url += "&"+arr.join("&");
}
var head =document.getElementsByTagName("head")[0];
var scriptNode = document.createElement("script");
scriptNode.src = _url;
head.appendChild(scriptNode); }

事件绑定/发布

 function EventMachine(){
var _o = {};
this.bind = function(e,cb){
if(!_o[e]){
_o[e] = [];
}
_o[e].push(cb);
}
this.trigger = function(e,param){
if(arguments.length==2&&Object.prototype.toString.call(param) == "[object Array]"){
var arg = param;
}else{
var arg = Array.prototype.slice.call(arguments,1);
}
if(_o[e]&&_o[e].length>0){
for(var i=0,l=_o[e].length;i<l;i++){
_o[e][i].apply(this,arg);
}
}
}
}
上一篇:numpy交换列


下一篇:分析解剖微服务系列(二)-SOA和微服务异同