简体   繁体   English

调用内部对象函数javascript

[英]Calling inner objects function javascript

I'm going throug book by Niklas Zakas. 我要翻阅尼古拉斯·扎卡斯(Niklas Zakas)的书。 I'm riding about custom events and currently going through this code: 我正在讨论自定义事件,目前正在执行以下代码:

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 + ")";
    });

My question is about calling function DragDrop.enable(); 我的问题是关于调用函数DragDrop.enable();

I don't get it how can we call an enable function which is not defined directly on DragDrop but on dragdrop bject (defined at the top inside DragDrop). 我不明白该如何调用一个不是直接在DragDrop上定义但在dropdrop bject(在DragDrop内部顶部定义)上的启用函数。

Thanks for any suggestion or point to some reading.. 感谢您的任何建议或指向一些阅读材料。

The DrapDrop variable is assigned the dragdrop object (due to the return statement) which has a property enable() 为DrapDrop 变量分配了具有属性enable()的拖放对象 (由于return语句)

So to call enable, simply DragDrop.enable() 因此要调用启用,只需拖动DragDrop.enable()

The code is using an immediately invoked function expression (IIFE) to setup some variables held in a closure. 该代码使用立即调用的函数表达式(IIFE)来设置闭包中保存的某些变量。

At the start of the assignment: 在分配开始时:

var DragDrop = function(){

most would write it as: 大多数人会这样写:

var DragDrop = (function(){

where the extra "(" makes it clear that the function expression will be executed and the result assigned, not the function itself. 多余的“(”表示将执行函数表达式并分配结果,而不是函数本身。

The dragdrop object is created here: 拖放对象在此处创建:

    var dragdrop = new EventTarget(),

and properties assigned here: 和此处分配的属性:

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

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

then the object that will be assigned to DragDrop (and is currently referenced by dragdrop ) is returned here: 然后将在此处返回将分配给DragDrop的对象(当前由dragdrop引用):

    return dragdrop;

And finally, the empty parameter list causes the function expression to be exucuted: 最后,空参数列表会导致函数表达式被推导:

}();

which would more commonly be written: 通常会这样写:

}());

to balance the extra "(" suggested earlier. So DragDrop now references the returned object (an instance of EventTarget ) which has access to all the variables created in the IIFE (such as drag , diffX , diffY ) but other functions don't. 为了平衡前面提到的多余的“(”。因此, DragDrop现在引用了返回的对象( EventTarget的实例),该对象可以访问在IIFE中创建的所有变量(例如dragdiffXdiffY ),而其他函数则没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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