gpt4 book ai didi

javascript - 原型(prototype)属性javascript的问题

转载 作者:行者123 更新时间:2023-11-30 18:49:32 24 4
gpt4 key购买 nike

我在尝试让它运行时遇到了问题。我正在尝试使用观察者模式。我希望能够在选择更改事件上运行任意数量的函数。我不断收到错误“Publisher.protoype”为空或不是对象。我做错了什么?

        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>

最佳答案

您有一个拼写错误:Publisher.prototype.delivery 缺少一个“t”。

关于javascript - 原型(prototype)属性javascript的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4386651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com