I'm new to JavaScript and I am trying to create a MapManager class that instantiates a few other Managers that will manage different aspects of a Google map; such as markers, different views, etc.
The problem that I am having is that when I attempt to pass "this" as a parameter to the other classes, that the MapManager is instantiating, it only passes an empty instance of MapManager (The JavaScript console shows "MapManager { }" and I get errors when calling the functions declared inside of the MapManager class.)
var MapManager = function(map) {
var markerManager = new MarkerManager(this);
var viewManager = new ViewManager(this);
var getMarkerManager = function() {
return markerManager;
}
var getViewManager = function() {
return viewManager;
}
var getMap = function() {
return map;
}
return {
getMarkerManager: getMarkerManager,
getViewManager: getViewManager,
getMap: getMap
}
}
var MarkerManager = function(mapManager) {
var map = mapManager.getMap();
var markers = {};
var createMarker = function(id, latitude, longitude) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(latitude, longitude),
map: map,
animation: google.maps.Animation.DROP
});
marker.set("id", id);
markers[id] = marker;
var listener = new google.maps.event.addListener(marker, "click", function () {
mapManager.getViewManager().setZoomToClient(id);
};
};
return {
createMarker: createMarker
};
}
var ViewManager = function(mapManager) {
var map = mapManager.getMap();
var setBoundView = function() {
var bounds = new google.maps.LatLngBounds();
var markers = mapManager.getMarkerManager.getMarkers();
for(id in markers) {
bounds.extend(markers[id].getPosition());
}
map.fitBounds(bounds);
};
return {
setBoundView: setBoundView
};
}
Am I not able to pass "this" in JavaScript for Dependency Injection? If you are able to compare the differences between "this" in JavaScript as opposed another language then it may be useful to know that I am familiar with PHP, Java and Python.