上次对
js自定义事件做了一些介绍,这次修改prototype使得Event对象支持observe任何对象,代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
var Event =
{
observers: false,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
observe: function(element, name, observer, useCapture)
{
if(typeof element == 'string')
element = document.getElementById(element);
useCapture = useCapture || false;
if (name == 'keypress' &&
(navigator.appVersion.match(/Konqueror|Safari|KHTML/)
|| element.attachEvent))
name = 'keydown';
this._observeAndCache(element, name, observer, useCapture);
},
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
_observeAndCache: function(element, name, observer, useCapture)
{
alert(this)
if (!this.observers) this.observers = [];
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(element.tagName)
{
this.observers.push([element, name, observer, useCapture]);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (element.addEventListener)
{
element.addEventListener(name, observer, useCapture);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else if (element.attachEvent)
{
element.attachEvent('on' + name, observer);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else if(typeof element[name] == 'function')
{
var s = element[name].toString();
s = s.substring(0, s.lastIndexOf('}')) + 'Event.occor(this,\'' + name + '\')'
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
s = s.replace('function()
{', '')
s = s.replace(/return/img,'Event.occor(this, \'' + name + '\');return')
element[name] = new Function(s)
this.observers.push([element, name, observer, useCapture]);
}
},
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
occor:function(obj, method)
{
var arr = [];
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(var i=0; i<Event.observers.length; i++)
{
if(Event.observers[i][0]==obj && Event.observers[i][1]==method) arr.push(Event.observers[i][2]);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(var i=0; i<arr.length; i++)
{
arr[i]();
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function F()
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.method = function()
{
alert('f.method is called')
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(i=0; i<10; i++)
{
}
return;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
var f = new F()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Event.observe(f, 'method', function()
{alert(1)}, false)
f.method()
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
当然如果你不想修改prototype,也可以参考下面的代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
var Observer =
{
__list:[],
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
observe:function(obj, pro, fun, b)
{
if(typeof obj[pro] != 'function')
return;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.__list.push(
{o:obj, p:pro, f:fun})
var s = obj[pro].toString();
s = s.substring(0, s.lastIndexOf('}')) + 'Observer.__occor(this,\'' + pro + '\')'
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
s = s.replace('function()
{', '')
if(b)
s = s.replace(/return/img,'Observer.__occor(this, \'' + pro + '\');return')
document.write(s)
obj[pro] = new Function(s)
},
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
__occor:function(obj, method)
{
var arr = []
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(var i=0; i<this.__list.length; i++)
{
if(Observe.__list[i].o==obj && Observer.__list[i].p==method) arr.push(Observer.__list[i]);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(var i=0; i<arr.length; i++)
{
arr[i].f();
}
}
}