Javascript js singleton-如何避免通过名称空间访问类成员
我正在努力改进我的Javascript编码风格,并且一直在读到使用名称空间是一件好事 然而,我似乎不能在任何我想在的地方使用“this”关键字,相反,我只能从匿名函数中通过名称空间(在本例中为“oldMap”)访问我的类属性。这意味着我不能在不更改代码的情况下更改名称空间id——这似乎是错误的 这是我创建的类-它实际上似乎工作正常。(对不起,太长了) 对于我所做的正确/错误的任何建议/提示,我们都非常感激。谢谢Javascript js singleton-如何避免通过名称空间访问类成员,javascript,singleton,this,javascript-namespaces,Javascript,Singleton,This,Javascript Namespaces,我正在努力改进我的Javascript编码风格,并且一直在读到使用名称空间是一件好事 然而,我似乎不能在任何我想在的地方使用“this”关键字,相反,我只能从匿名函数中通过名称空间(在本例中为“oldMap”)访问我的类属性。这意味着我不能在不更改代码的情况下更改名称空间id——这似乎是错误的 这是我创建的类-它实际上似乎工作正常。(对不起,太长了) 对于我所做的正确/错误的任何建议/提示,我们都非常感激。谢谢 var oldMap = { map : null, cente
var oldMap = {
map : null,
center : {lat:50, lng:20, zoom:3},
drawn : false,
data : {},
divId : "oldMap",
url : "php/getMapData.php",
infowindow : new google.maps.InfoWindow({
size: new google.maps.Size(150,50)
}),
init : function () {
if (!this.drawn){
$.getJSON(
this.url,
function(d){
if(d){
$.extend(oldMap.data,d);
var latlng = new google.maps.LatLng(oldMap.center.lat, oldMap.center.lng);
var myOptions = {
zoom: oldMap.center.zoom,
center: latlng,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
// create the map
map = new google.maps.Map(document.getElementById("oldMap"),myOptions);
// create the legend
var legendDiv = document.createElement('DIV');
legendDiv.innerHTML = '<div id="legend"><img src="images/markers/legend-blur.png"></div>';
map.controls[google.maps.ControlPosition.TOP_LEFT].push(legendDiv);
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});
// Set the info window html template
var infoWindowTemplate = "<div id='balloon'>{{#url2}}<img src='{{url2}}' />{{/url2}}<h2>{{project_name}}</h2><p><b>Amount</b> € {{cost}}</p><p><b>Country</b> {{country}}</p><p><b>Year</b> {{year}}</p><p><b>Project Type</b> {{project_type}}</p><p>{{description}}</p>{{#url}}<p><a target='_blank' href='{{url}}'>More info</a></p>{{/url}}</div>"
// loop through the projects
for(var m in oldMap.data) {
// if the project has a marker type defined
if (oldMap.data[m].marker) {
// point
var point = new google.maps.LatLng(oldMap.data[m].lat, oldMap.data[m].longtd);
// create HTML for info window
var infoHtml = Mustache.to_html(infoWindowTemplate, oldMap.data[m]);
// icon
var icon = new google.maps.MarkerImage(
"images/markers/33px/" + oldMap.data[m].marker + ".png",
new google.maps.Size(33,33)
);
// create a marker for this project
var marker = oldMap.createMarker(point,infoHtml,icon);
}
}
oldMap.drawn = true;
}
}
)
}
},
createMarker : function (latlng, html, icon) {
// create the marker
var marker = new google.maps.Marker({
position: latlng,
icon: icon,
map: map,
zIndex: Math.round(latlng.lat()*-100000)<<5
});
// open info window when marker clicked
google.maps.event.addListener(marker, 'click', function() {
oldMap.infowindow.setContent(html);
oldMap.infowindow.open(map,marker);
});
}
};
var oldMap={
map:null,
中心:{lat:50,lng:20,zoom:3},
答案:错,
数据:{},
divId:“旧地图”,
url:“php/getMapData.php”,
infowindow:new google.maps.infowindow({
尺寸:新谷歌地图尺寸(150,50)
}),
init:函数(){
如果(!this.draw){
$.getJSON(
这个.url,
职能(d){
如果(d){
$.extend(oldMap.data,d);
var latlng=新的google.maps.latlng(oldMap.center.lat,oldMap.center.lng);
变量myOptions={
缩放:oldMap.center.zoom,
中心:拉特林,
mapTypeId:google.maps.mapTypeId.TERRAIN
};
//创建地图
map=new google.maps.map(document.getElementById(“oldMap”),myOptions);
//创建图例
var legendDiv=document.createElement('DIV');
legendDiv.innerHTML='';
map.controls[google.maps.ControlPosition.TOP_LEFT].push(legendDiv);
google.maps.event.addListener(映射,'click',函数(){
infowindow.close();
});
//设置信息窗口html模板
var infoWindowTemplate=“{{{{url2}}{{/url2}}{{project{u name}}金额和欧元{{cost}}国家{Country}年度{Year}项目类型{project类型}zIndex:Math.round(latlng.lat()*-100000)直接在对象上的函数的第一行应该是
function () {
var that = this;
...
}
然后,在内部函数中,将对this
的引用与that
交换
这是因为内部函数“此
指向窗口
如果您使用的是jQuery,请查看proxy()方法:
此方法专门设计用于将This
的范围强制到特定对象。我个人更喜欢PrototypeJS bind()的语法:
…虽然我更喜欢PrototypeJS而不是jQuery,但这场战斗似乎已经打过了,而且已经失败了