调用内部对象函数javascript

时间:2014-06-18 22:09:20

标签: javascript methods

我要通过Niklas Zakas的书。我正在谈论自定义事件,目前正在浏览此代码:

var DragDrop = function(){

        var dragdrop = new EventTarget(),
            dragging = null,
            diffX = 0,
            diffY = 0;

        function handleEvent(event){

            //get event and target
            event = EventUtil.getEvent(event);
            var target = EventUtil.getTarget(event);            

            //determine the type of event
            switch(event.type){
                case "mousedown":
                    if (target.className.indexOf("draggable") > -1){
                        dragging = target;
                        diffX = event.clientX - target.offsetLeft;
                        diffY = event.clientY - target.offsetTop;
                        dragdrop.fire({type:"dragstart", target: dragging, x: event.clientX, y: event.clientY});
                    }                     
                    break;

                case "mousemove":
                    if (dragging !== null){

                        //assign location
                        dragging.style.left = (event.clientX - diffX) + "px";
                        dragging.style.top = (event.clientY - diffY) + "px";   

                        //fire custom event
                        dragdrop.fire({type:"drag", target: dragging, x: event.clientX, y: event.clientY});
                    }                    
                    break;

                case "mouseup":
                    dragdrop.fire({type:"dragend", target: dragging, x: event.clientX, y: event.clientY});
                    dragging = null;
                    break;
            }
        };

        //public interface
        dragdrop.enable = function(){
                EventUtil.addHandler(document, "mousedown", handleEvent);
                EventUtil.addHandler(document, "mousemove", handleEvent);
                EventUtil.addHandler(document, "mouseup", handleEvent);
        };

        dragdrop.disable = function(){
                EventUtil.removeHandler(document, "mousedown", handleEvent);
                EventUtil.removeHandler(document, "mousemove", handleEvent);
                EventUtil.removeHandler(document, "mouseup", handleEvent);
        };

        return dragdrop;
    }();

    DragDrop.enable();

    DragDrop.addHandler("dragstart", function(event){
        var status = document.getElementById("status");
        status.innerHTML = "Started dragging " + event.target.id;
    });

    DragDrop.addHandler("drag", function(event){
        var status = document.getElementById("status");
        status.innerHTML += "<br>Dragged " + event.target.id + " to (" + event.x + "," + event.y + ")";
    });

    DragDrop.addHandler("dragend", function(event){
        var status = document.getElementById("status");
        status.innerHTML += "<br>Dropped " + event.target.id + " at (" + event.x + "," + event.y + ")";
    });

我的问题是关于调用函数DragDrop.enable();

我不知道如何调用一个启用函数,该函数不是直接在DragDrop上定义的,而是在dragdrop bject上定义的(在DragDrop的顶部定义)。

感谢您提出任何建议或指出阅读..

2 个答案:

答案 0 :(得分:0)

DrapDrop 变量被赋予了拖拉对象(由于返回语句),其具有属性enable()

所以要调用enable,只需DragDrop.enable()

答案 1 :(得分:0)

代码使用一个立即调用的函数表达式(IIFE)来设置闭包中保存的一些变量。

在作业开始时:

var DragDrop = function(){

大多数人会把它写成:

var DragDrop = (function(){

其中“(”表示将执行函数表达式并分配结果,而不是函数本身。

dragdrop 对象在此处创建:

    var dragdrop = new EventTarget(),

和此处指定的属性:

    //public interface
    dragdrop.enable = function(){
      ...
    };

    dragdrop.disable = function(){
      ...
    };

然后将返回将分配给 DragDrop 的对象(当前由 dragdrop 引用):

    return dragdrop;

最后,空参数列表会导致函数表达式被删除:

}();

更常见的是:

}());

平衡额外的“(”之前建议。所以 DragDrop 现在引用返回的对象( EventTarget 的一个实例),它可以访问在IIFE(例如拖动 diffX diffY )但其他功能没有。