【问题标题】:I don't understand this part of the javascript by prototype我不理解原型的这部分javascript
【发布时间】:2011-12-02 19:52:21
【问题描述】:

我目前正在使用 cakephp 和 prototype.js 开发谷歌地图 api V3。我有一个名为:TravelMapprManager 的 javascript 类,它有 4 个类变量和 18 个函数。

var TravelMapprManager = Class.create( {

  // id of container
    map_container : '',

  /* the current map */
    map : null,

  /* geocoding location */
    geocoder : null,

   /* user entered locations */
     user_journey : new Array(),

  //many other functions [.....]

initialize : function( map_container ) {

    this.map_container = map_container;

    // start the map
    Event.observe( window, 'load', this.displayMap.bind(this) );

    // observe the map buttons
    Event.observe( document, 'dom:loaded', this.initObservers.bind(this) );

},

   /*
    * Save the location entered
    */
findLocation : function() {

    location_name = $( 'LocationName' ).value;


    if ( location_name == '' ) {
        alert( "Please enter a location name." );
        return;            
    }

    // we only allow a maximum number of locations
    if ( this.user_journey.length >= 20 ) {
        alert( "Sorry! We have reached the maximum number of locations." );
        return;
    }

    // Do geocoding, find the longitude and latitude of the location
    if ( this.geocoder ) {

        var current_o = this;

        this.geocoder.getLatLng(
            location_name,
            function( point ) {

                if ( !point ) {
                    alert( location_name + " not found" );
                } else {

                    // store the location
                    current_o.storeLocation( location_name, point );

                    // center the location on the map and add push pin marker
                    current_o.map.setCenter( point, 13 );
                    var marker = new GMarker( point );
                    current_o.map.addOverlay( marker );
                }
            }
            );
        }
    }
})

函数 findLocation 中的var current_o = this; 是什么意思?

【问题讨论】:

    标签: javascript cakephp-1.3 prototypejs


    【解决方案1】:

    this inside function findLocationthis internal function 关键字不同:

    var current_o = this; //<-- Store reference to the `this` keyword inside the func
    ...
    this.geocoder.getLatLng(
            location_name,
            function( point ) { //<------------ 
    ....
    

    通过将this 关键字存储在临时变量中,内部函数还可以访问this 内部函数findLocation 的属性。


    一个简单的例子。此代码将事件侦听器添加到下一个输入元素,同时保持对前一个元素的引用:
    var a = document.getElementsByTagName("input");
    a[0].onclick = function(){
        var firstElement = this; //First `this`
        a[1].onclick= function(){
            firstElement.onclick = function(){}; //Reset
            this.onclick = function(){alert("This is different!")}; //Second `this`
        }
    }
    

    事件侦听器中的this 关键字指的是它们绑定到的元素。在示例中,第一个 this 引用元素 input[0],而第二个 this 引用 input[1]。当您不将第一个 this 存储在临时变量 (firstElement) 中时,您将无法引用之前的 this(而不是通过 document.getElements.. 直接引用第一个元素)。

    【讨论】:

    • 你帮助我消除了疑虑,激发了我的思维能力
    【解决方案2】:

    它将this 绑定到局部变量,以便可以在传递给getLatLng 的匿名函数中使用该对象。这是必要的,因为 this 的引用对象在该函数内部可能不同 - 这取决于它在 getLatLng 内部的调用方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多