简体   繁体   中英

Using addEventListener on Prototype

i am trying to runt the results of Prototype via a click through addEventListener but the results are showing even without clicking.

<div id="box">Your Item:</div>

<button id="myBtn">Buy</button>

<script type="text/javascript">
function Machine(n,p){
this.name = n;
this.price = p;
}


Machine.prototype.Dispatch = function(){
var container = document.getElementById('box');
container.innerHTML = this.name + " " + this.price;
}
var Drink = new Machine("coke",80);
var Handle = document.getElementById('myBtn');
Handle.addEventListener("click",Drink.Dispatch(),false);
</script>

http://jsfiddle.net/9trqK/

You have to pass a function reference to addEventListener , instead of calling the function (and passing its return value) as you're doing:

Handle.addEventListener("click", Drink.Dispatch, false);

When you do that, your handler will only be fired on click. But you will face a different problem: this inside the handler will be the clicked element, not your Machine instance. To solve that, you can use Function.bind (see the linked docs for a polyfill for older browsers):

var drink = new Machine("coke",80);
var handle = document.getElementById('myBtn');
handle.addEventListener("click", drink.Dispatch.bind(drink), false);

Note: It's common practice to only use uppercase initials for constructor function names, so you can tell them apart on a quick glance. That's why I renamed Drink and Handle to drink and handle .

You need to add your call between a function() {} closure for your listener. Or remove the () after your function name.

This should work: http://jsfiddle.net/9trqK/1/

function Machine(n, p) {
  this.name = n;
  this.price = p;
}
var handle = document.getElementById('myBtn');
Machine.prototype.Dispatch = function () {
  var container = document.getElementById('box');
  container.innerHTML = this.name + " " + this.price;
}
var Drink = new Machine("coke", 80);
handle.addEventListener("click", function() { Drink.Dispatch() }, false);

To do custom events on object I use this simple script to extend my objects:

/* Simple Custom event prototype for objects

Extend your object like this:

<OBJECT>.prototype = new EventEmitter;

Then you can use :

<OBJECT>.on("test",function() { alert("Test event triggered"); })
<OBJECT>.on("test",function() { alert("Test event 2 triggered"); })
<OBJECT>.trigger("test");

*/
function EventEmitter() {

    var listeners = Object();   //Matrix of event/callbacks

    //Add listener
    this.on = function(evt, callback) {
        //console.log("Listener added: " + evt);

        if (!listeners.hasOwnProperty(evt))
            listeners[evt] = Array();

        listeners[evt].push(callback);      
    }

    //Call listeners
    this.trigger = function(evt, params) {
        //console.log("trigger called " + evt);
        //console.dir(listeners);

        if (evt in listeners) {
            callbacks = listeners[evt];
            //Call all callbacks with the params
            for (var x in callbacks){
                callbacks[x](params);
            }
        } else {
            console.log("No listeners found for " + evt);
        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM