1

我不知道该怎么称呼这个问题,因为它有点大。

我从基本模板 Phonegap 下载了代码。

有一个代码示例:

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 explicity 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.receivedEvent('deviceready');” 而不是“this.receivedEvent('deviceready');”

我试图理解,但我就是无法理解。

任何人都可以向我解释这个?

谢谢 :)

4

2 回答 2

2

评论解释了原因。

您需要通知应用程序设备已准备就绪。上下文中的this对象就是事件本身。

这就像打电话一样event.receivedEvent

您需要通知应用程序设备已准备好,因此需要app调用。

编辑:为了进一步澄清,this是应用程序对象的上下文,app除了onDeviceReady函数。该bindEvents函数运行以下代码:

document.addEventListener('deviceready', this.onDeviceReady, false);

它绑定onDeviceReady到一个事件,通过该事件将fromaddEventListener的上下文更改为.thisappevent

这意味着,您不能通过具有更改上下文app的关键字调用方法。this希望这有助于澄清更多。

编辑2:查看此代码进行一些演示:http: //jsbin.com/UGerika/1/edit ?html,js,output

    // http://jsbin.com/UGerika/1/edit?html,js,output
    // Demonstrates the scope of 'this' variable once a
    // function is bound to event receiver.

    var app = {
      var1: function(){
       return "i am this"; 
      },

        initialize: function() {
            this.bindEvents();
        },

        bindEvents: function() {
          alert('bindevents');   
          this.onWindowClick();
        },

      onWindowClick: function() {
        // in this case, this.var1 is perfection valid
        // 'this' is still scoped to 'app' object.
        alert(this.var1);   
      }

    };


    var app2 = {
      var1: function(){
       return "i am this"; 
      },

        initialize: function() {
            this.bindEvents();
        },

        bindEvents: function() {
          alert('bindevents');   
          document.addEventListener('mousedown', this.onWindowClick, false);
        },

      onWindowClick: function() {
        // in this case, this.var1 is invalid
        // 'this' is scoped to 'mousedown' event.
        // we would have to call app2.var1
        alert(this.var1);   
      }

    };
于 2013-09-30T16:50:38.087 回答
0

我不熟悉phonegap应用程序。但从评论中我了解到,函数 onDeviceReady 的上下文是实际事件,而不是应用程序本身。所以你需要明确地使用 app 而不是 this 来引用实际的应用程序。

于 2013-09-30T16:50:34.260 回答