0

I am trying the understand the concept of adding event processing feature (at page no. 120) to objects explained by Crockford as in this example:

var eventuality = function (that) {
    var registry = {};

    that.fire = function (event) {
        var array,
                func,
                handler,
                i,
                type = typeof event === 'string' ? event : event.type;

        if (registry.hasOwnProperty(type)) {
            array = registry[type];
            for (i = 0; i < array.length; i += 1) {
                handler = array[i];
// A handler record contains a method and an optional
// array of parameters. If the method is a name, look
// up the function.
                func = handler.method;
                if (typeof func === 'string') {
                    func = this[func];
                }
// Invoke a handler. If the record contained
// parameters, then pass them. Otherwise, pass the
// event object.
                func.apply(this,
                        handler.parameters || [event]);
            }
        }
        return this;
    };

    that.on = function (type, method, parameters) {
// Register an event. Make a handler record. Put it
// in a handler array, making one if it doesn't yet
// exist for this type.
        var handler = {
            method: method,
            parameters: parameters
        };
        if (registry.hasOwnProperty(type)) {
            registry[type].push(handler);
        } else {
            registry[type] = [handler];
        }
        return this;
    };
    return that;
};

console.log('eventuality ', eventuality({name: "test"}));

Well, it adds methods on and fire but how do I use it in practical? I mean, it was briefly explained that on and fire events used for handling events when fired but didn't find an example on how to put it to implementation?

If any one could explain what is happening in the above example, it would be helpful. Thank you

kittu
  • 6,662
  • 21
  • 91
  • 185
  • @mplungjan Which one is the new version. I am not able to find on amazon as well? – kittu Oct 08 '17 at 13:10
  • You are correct. I could not either – mplungjan Oct 08 '17 at 13:11
  • 1
    You create an event manager `var test = eventuality({name: "test"});` then you can add events with `test.on("T",()=>{console.log("Myevent")}, {data : "myEventData"})` Which you can then fired with `text.fire("T")` Its just a way of creating function calls with associated data . Rather old school and not really relevant these days. – Blindman67 Oct 08 '17 at 13:24
  • @Blindman67 What do you recommend then? I started with Mr.Crockfords book as it had very good reviews. – kittu Oct 08 '17 at 13:45
  • @SatyaDev Crockford is a great programmer, but the books is near 10 years old, that is forever. Get acquainted with ES6 and ES7. Do you know how to use arrow function, the spread operator, used destructuring, default parameters, async functions, and many many new features of javascript that never get a mention in the book but are a must know in modern javascript. Always check the publish date if you are looking for learning material. – Blindman67 Oct 08 '17 at 14:23
  • @Blindman67 I'm trying to strengthen on basics first though I know few things in es6 – kittu Oct 08 '17 at 16:53

0 Answers0