phonegap javascript模板说明寻求使用此关键字

时间:2013-09-30 16:46:01

标签: javascript cordova

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

我从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');”

我试图了解但我无法得到它。

任何人都可以向我解释一下吗?

谢谢:)

2 个答案:

答案 0 :(得分:2)

评论解释了原因。

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

就像打电话给event.receivedEvent

您需要通知应用程序设备已准备就绪,因此app呼叫。

编辑:为了进一步说明,thisapp与app onDeviceReady功能相同的上下文。 bindEvents函数运行此代码:

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

通过onDeviceReadyaddEventListener与事件绑定,从而将this的上下文从app更改为event

这意味着,您无法通过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);   
      }

    };

答案 1 :(得分:0)

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