我没有通过原型来理解javascript的这一部分
I don't understand this part of the javascript by prototype
我目前正在使用cakephp与prototype.js开发谷歌地图api V3。我有一个javascript类叫做: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 );
}
}
);
}
}
})
在函数findLocation中var current_o = this;
意味着什么?
this
内部函数findLocation
与内部函数this
关键字不同:
var current_o = this; //<-- Store reference to the `this` keyword inside the func
...
this.geocoder.getLatLng(
location_name,
function( point ) { //<------------
....
通过将this
关键字存储在临时变量中,内部函数也可以访问findLocation
内部函数this
的属性。
一个简单的例子。以下代码将事件侦听器添加到下一个输入元素,同时保持对前一个元素的引用:
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..
的第一个元素)。
它将this
绑定到局部变量,以便该对象可以在传递给getLatLng
的匿名函数中使用。这是必要的,因为this
的指针在该函数中可能是不同的——这取决于它在getLatLng
中如何调用。
相关文章:
- 如何使用JavaScript提取此url的一部分
- 通过JavaScript设置的表单字段的值不会作为$_POST的一部分传递给PHP脚本
- href没有在每次单击li的一部分时加载链接.使用javascript使整个li可以点击
- 是否可以将此javascript代码(或其中的一部分)移动到单独的文件中,而不是内联
- Javascript 解包元素的一部分
- JQuery/JavaScript - 突出显示输入或文本区域中的一部分文本
- 如何知道找到的单词是字符串的一部分还是JavaScript中的函数调用
- 将外部javascript文件视为Aspx页面的一部分
- JavaScript未更新$scope变量的一部分
- 当量词是模式的一部分时,JavaScript中的RegExp
- 返回javascript中regex字符串的一部分
- javascript:如何首先加载页面的一部分
- 在表单提交将DOM的一部分替换为分部之后,我应该将ajax成功绑定到什么来使我的javascript工作
- JavaScript:删除一部分代码,并在单击时删除一个元素
- javaScript代码在一个单独的文件中工作,但不是更大文件的一部分
- JavaScript Regexp仅匹配字符串的一部分
- Javascript语句的哪一部分是字面意思
- 撇号标记在javascript字符串中与Backslash一起发送,该字符串使用ajax作为json对象的一部分发送到p
- 如何返回“;成品“;javascript代码的一部分
- JavaScript:将字符串的一部分放入DOM容器中,保存溢出