phonegap index.js 文件为什么 app.receivedEvent 不是 this.receivedEvent

phonegap index.js file why app.receivedEvent not this.receivedEvent

谁能告诉我为什么在下面的代码中使用了 app.receivedEvent('deviceready'),而不是 this.receivedEvent('deviceready')

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};

评论实际上解释了使用 app 而不是 this 的原因:

// The scope of 'this' is the event. In order to call the 'receivedEvent' 
// function, we must explicitly call 'app.receivedEvent(...);'

基本上,事件处理程序将使用自己的 this 上下文调用事件回调(其中 this 将是事件对象)。

方法 receivedEvent 是在应用程序中定义的,而不是 this(设置为事件回调函数内的事件对象)。在这种情况下要调用 receivedEventil,您可以将其作为包含对象 app:

的方法来调用
app.receivedEvent(...);