1
我在尝试运行时遇到了问题。我正在尝试使用观察者模式。我不能在select change事件上运行一个仲裁函数。我不断收到错误“Publisher.protoype”为空或不是对象。我究竟做错了什么?原型属性问题javascript
function Publisher(){
this.subscribers = [];
}
Publisher.protoype.deliver = function(data){
this.subscribers.forEach(
function(fn){
fn(data);
}
);
return this;
}
Function.prototype.subscribe = function(publisher){
var that = this;
var AlreadyExists = publisher.subscribers.some(
function(el){
if (el == that){
return;
}
}
);
if(!AlreadyExists){
publisher.subscribers.push(this);
}
return this;
}
Function.prototype.unsubscribe = function(publisher){
var that = this;
publisher.subscribers = publisher.subscribers.filter(
function(el){
if(el != that){
return el;
}
}
);
return this;
}
var EventPublisher = new Publisher();
var SelectChange = function(data){alert("hello")};
SelectChange.subscribe(EventPublisher);
function onSelectChange(oSelect){
EventPublisher.deliver(oSelect);
}
</script>
</head>
<body>
<form name="Tester" action="Tester" method="post" enctype="application/x-www-form-urlencoded">
<select name="selecter" onchange="Javascript:onSelectChange(this)">
<option name="Shane" value="Shane">
Shane
</option>
<option name="Shane2" value="Shane2">
Shane2
</option>
</select><input type="submit"><input type="reset">
</form>
</body>
</html>
那么这值得一脸巴掌! :P感谢! – Shane 2010-12-08 11:01:38