Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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
道格拉斯·克罗克福德';s";Javascript:好的部分;第5.5章_Javascript - Fatal编程技术网

道格拉斯·克罗克福德';s";Javascript:好的部分;第5.5章

道格拉斯·克罗克福德';s";Javascript:好的部分;第5.5章,javascript,Javascript,我正在读这本书的第5.5章。我仍然很难理解如何使用本章中的偶然性函数“我们可以将对象组成一组部分” 对象是否由具有“开”和“火”功能的事件系统组成 代码取自本书以下章节: var eventuality = function (that) { var registry = {}; that.fire = function (event) { // Fire an event on an object. The event can be either // a string con

我正在读这本书的第5.5章。我仍然很难理解如何使用本章中的偶然性函数“我们可以将对象组成一组部分”

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

代码取自本书以下章节:

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;
}
var eventity=function(that){
var注册表={};
that.fire=功能(事件){
//在对象上激发事件。该事件可以是
//包含事件或事件名称的字符串
//对象,该对象包含包含
//由“on”注册的事件处理程序的名称
//将调用与事件名称匹配的方法。
变量数组,
func,
handler,
我
类型=事件类型==='string'?
event:event.type;
//如果此事件存在处理程序数组,则
//循环遍历它并按顺序执行处理程序。
if(registry.hasOwnProperty(类型)){
数组=注册表[类型];
对于(i=0;i
任何一种方法都是可能的。我是这位作家的超级粉丝,他对我来说就像个英雄。无论如何,javascript为我们提供的最好的特性之一就是动态对象。我们可以根据需要创建对象

据我所知,本书这一节的重点是说明JavaScript的强大功能,因为您可以轻松地用各种“强大的JavaScript组件”构建一个对象

正如他所说

例如,我们可以制作一个函数 这可以添加简单的事件处理 任何对象的特征。它增加了一个 方法、fire方法和private方法 事件注册表


Crockford先生在这里的意思是,您可以实现特定的功能,例如
on
fire
函数,这些函数通过调用创建它们的函数对象(
在本例中为eventity
)将事件处理添加到任何对象中,并将该对象作为参数

这里的“部分”是体现在
eventity
函数对象中的“事件处理部分”。您可以想象添加其他功能的不同部分。这里的想法是,您可以使用此系统将此功能添加到需要的单个对象中。这个概念称为a(1)

同时阅读第5章最后一段:

通过这种方式,构造函数可以从一组部件组装对象。JavaScript的松散类型在这里是一个很大的好处,因为我们不需要担心类的沿袭的类型系统


(1) 多谢各位

以下是我自己的测试结果。如果复制代码并将其粘贴到名为“test.js”的文件中,然后通过“node test.js”在命令行上运行它(必须已经安装了node),您将得到相同的结果。我的工作是通过使用自我解释的注释跟踪流,向您展示如何使用eventity()

我唯一不明白的地方就是那条线;“基金=本[func]”(注释为“?”)。似乎“func=registry[func]”对我来说更有意义,因为registry对象是注册处理程序的地方,而不是eventity函数对象(即“this”)

var eventity=function(that){
var注册表={};
that.fire=功能(事件){
var type=typeof event=='string'?event:event.type;
log('fire():在事件“'+type+'”时激发);
if(registry.hasOwnProperty(类型)){
var数组=注册表[类型];
对于(变量i=0;i