原型属性javascript的问题

时间:2010-12-08 10:56:20

标签: javascript prototype

我无法让它运行起来。我试图使用观察者模式。我想能够在select change事件上运行一定数量的函数。我一直收到错误'Publisher.protoype'为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>

2 个答案:

答案 0 :(得分:2)

你有一个错字:Publisher.protoype.deliver缺少't'。

答案 1 :(得分:0)

Publisher.protoype绝对是null或不是对象。也许您打算输入Publisher.prototype