Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
原型属性javascript存在问题_Javascript_Prototype - Fatal编程技术网

原型属性javascript存在问题

原型属性javascript存在问题,javascript,prototype,Javascript,Prototype,我很难让它运行。我正在尝试使用观察者模式。我希望能够在选择更改事件上运行任意数量的函数。我一直收到一个错误“Publisher.prototype”为null或不是对象。我做错了什么 function Publisher(){ this.subscribers = []; } Publisher.protoype.deliver = function(data){ this.subscribers.

我很难让它运行。我正在尝试使用观察者模式。我希望能够在选择更改事件上运行任意数量的函数。我一直收到一个错误“Publisher.prototype”为null或不是对象。我做错了什么

        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>

        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>
函数发布器(){
this.subscribers=[];
}
Publisher.prototype.deliver=函数(数据){
这是我的订户(
功能(fn){
fn(数据);
}
);
归还这个;
}
Function.prototype.subscribe=函数(发布者){
var=这个;
var AlreadyExists=publisher.subscribers.some(
功能(el){
如果(el==该值){
返回;
}
}
);
如果(!AlreadyExists){
publisher.subscribers.push(此);
}
归还这个;
}
Function.prototype.unsubscribe=函数(发布者){
var=这个;
publisher.subscribers=publisher.subscribers.filter(
功能(el){
如果(el!=那个){
返回el;
}
}
);
归还这个;
}
var EventPublisher=new Publisher();
var SelectChange=函数(数据){alert(“hello”)};
选择Change.subscribe(EventPublisher);
函数onSelectChange(oSelect){
EventPublisher.deliver(oSelect);
}
谢恩
Shane2

您有一个输入错误:
Publisher.prototype.deliver
缺少一个“t”。

Publisher.prototype
肯定为空或不是对象。也许你的意思是要键入出版商。prototype

那该挨耳光了!:P谢谢你@谢恩-在评论中也漏掉了一个t。可能需要检查键盘;)
        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>