English 中文(简体)
我不理解按原型分类的那部分 j印。
原标题:I don t understand this part of the javascript by prototype

目前,我正在使用原型(js)的面包房,绘制Gogle地图一皮五3。 我有一个叫作:TravelMapprManager的栏目,有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 );
                }
            }
            );
        }
    }
})

What does var current_o = this; mean in the function findLocation?

最佳回答

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

By storing the this keyword in a temporary variable, the inner function can also access properties of the this inside function findLocation.


A simple example. This code adds an event listener to the next input element, while maintaining a reference to the previous element:
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`
    }
}

The this keyword within activitieslecters refer to the content they re subject to. 举例来说,第1条<代码>凡<>t>>系指:input[0],而第2条<代码>内>指input***/code>。 当您不把第1条<代码>即存放在临时变量中时(firstElement/ ),你就不必提及先前的<代码>this(没有通过document.getElements.直接提及第1项内容)。

问题回答

当地变量的编号为this,以便该物体可在通过getLatLng的匿名功能内使用。 之所以有必要,是因为this可能在该职能范围内有所不同——这取决于它是如何在上要求的。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.