简体   繁体   English

关于Java事件处理的困惑

[英]confusion regarding event handling in Javascript

I am new to Javascript. 我是Java语言的新手。 while practicing i encounter a code regarding event handling. 在练习时,我遇到有关事件处理的代码。 Here is the code 这是代码

//This generic event handler creates an object called eventHandler
var etHandler = {};

if (document.addEventListener) {

    //eventHandler object has two methods added to it, add()and remove(),
    etHandler.add = function(element, eventType, eventFunction) {

        /**
         * The add method in each model determines whether the event type is a load event, meaning that
         *  the function needs to be executed on page load.
         */
        if (eventType == "load") {

            .......

        }
    };// end of eventHandler.add = function()

    etHandler.remove = function(element, eventType, eventFunction) {

        element.detachEvent("on" + eventType, eventFunction);

    }; //end of  etHandler.remove = function()
}

function sendAlert() {

    alert("Hello");

} //end of sendAlert()

function startTimer() {

    var timerID = window.setTimeout(sendAlert,3000);

}//end of startTimer()

var mainBody = document.getElementById("mainBody");

etHandler.add(mainBody, "load", function() {

                                startTimer();

                            }
             );

The questions that i want to ask are this. 我想问的问题是这个。 We create an empty object. 我们创建一个空对象。 var etHandler = {}; . It's fine. 没关系。 Then we are checking condition if (document.addEventListener) {} . 然后,我们检查条件if (document.addEventListener) {} we didn't add any event listener to the document, but this condition is true. 我们没有在文档中添加任何事件侦听器,但是这种情况是正确的。 Why this condition is returning true? 为什么此条件返回true? Then we write etHandler.add = function(element, eventType, eventFunction) {} . 然后我们编写etHandler.add = function(element, eventType, eventFunction) {} Why we are writing etHandler.add ? 为什么我们要编写etHandler.add etHandler object has no property, when we created it. 当我们创建它时,etHandler对象没有属性。 It's a null object. 这是一个空对象。 If we create etHandler like this 如果我们这样创建etHandler

var etHandler = {

    add: function() {

    },

    remove: function(){

    }
};

Then we can write etHandler.add . 然后,我们可以编写etHandler.add The same question is for etHandler.remove also. 同样的问题也适用于etHandler.remove。

Thanks 谢谢

The call if (document.addEventListener) is checking whether the browser supports this method. 调用if (document.addEventListener)正在检查浏览器是否支持此方法。 It is checking to see whether it exists on the document object. 它正在检查它是否存在于文档对象上。 This is called feature detection and is frequently used to detect differences between browsers. 这称为功能检测,通常用于检测浏览器之间的差异。

The call etHandler.add = function(element, eventType, eventFunction) defines the add method and creates it simultaneously. 调用etHandler.add = function(element, eventType, eventFunction)定义add方法并同时创建它。 It is basically the same as in your example. 它与您的示例基本相同。

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

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