Problem with prototype property javascript
I am having trouble trying to get this to run. I am tryi开发者_开发技巧ng to use the observer pattern. I wnant to be able to run a arbitry amount of functions on the select change event. I keep getting an error 'Publisher.protoype' is null or not an object. What am I doing wrong?
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>
You have a typo: Publisher.protoype.deliver
is missing a 't'.
Publisher.protoype
is definitely null or not an object. Perhaps you meant to type Publisher.prototype
.
精彩评论