Douglas Crockford的“Javascript:The Good Parts”第5.5章 [英] Douglas Crockford's "Javascript: The Good Parts" Chapter 5.5

查看:82
本文介绍了Douglas Crockford的“Javascript:The Good Parts”第5.5章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读书中的第5.5章。我仍然很难看到我们可以使用章节中的可能性函数来组合各部分中的对象。

I was reading Ch 5.5 of the book in title. I have still have trouble in seeing how "We can compose objects out of sets of parts" using the eventuality function in the chapter.

对象是否由事件组成具有开和开功能的系统?

Are objects to be composed by a event system with the "on" and "fire" functions ?

本书以下部分的代码:

var eventuality = function (that) {
    var registry = {};
    that.fire = function (event) {
// Fire an event on an object. The event can be either
// a string containing the name of the event or an
// object containing a type property containing the
// name of the event. Handlers registered by the 'on'
// method that match the event name will be invoked.
        var array,
            func,
            handler,
            i,
            type = typeof event === 'string' ?
                    event : event.type;
// If an array of handlers exist for this event, then
// loop through it and execute the handlers in order.
        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;
}


推荐答案

Crockford先生在这里的意思是你可以实现特定的功能,如 fire 函数,通过调用任何对象添加事件处理使用该对象作为参数创建它们的函数对象(在这种情况下为 eventuality )。

What Mr. Crockford means here is that you can implement specific functionality such as the on and fire functions that add event processing to any object by calling the function object that creates them (eventuality in this case) with that object as parameter.

这里的部分是 eventuality 函数对象中包含的事件处理部分。您可以想象添加其他功能的不同部分。这里的想法是您可以使用此系统将此功能添加到您需要的单个对象。这个概念被称为 Mixin (1)。

The "part" here is an "event processing part" embodied in the eventuality function object. You could imagine different parts that add other functions. The idea here is that you can use this system to add this functionality to individual objects where you need it. This concept is called a Mixin(1).

另请阅读第5章的最后一段:

Also read the final paragraph of chapter 5:


通过这种方式,构造函数可以从一组零件中组装对象。 JavaScript的松散输入在这里是一个很大的好处,因为我们不会担心关注类的谱系的类型系统。

In this way a constructor could assemble objects from a set of parts. JavaScript's loose typing is a big benefit here because we are not burdened with a type system that is concerned about the lineage of classes.

(1)谢谢 Zecc

这篇关于Douglas Crockford的“Javascript:The Good Parts”第5.5章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆