给定以下代码(StoriesMap.js)...
// Initalization; Creates Map
var StoriesMap = function StoriesMap( id, location, tileset ) {
this.map = L.map( id ).setView([ location.lat, location.lon ], location.zoom);
// Add inital points to map
this.populate( location );
// Redraw on map movement
this.map.on("viewreset", function( event ) {
var location = {
lat: event.target._animateToCenter.lat,
lon: event.target._animateToCenter.lng,
zoom: event.target._animateToZoom
};
this.redraw( location );
});
var mapTiles = L.tileLayer( tileset.url, { attribution: tileset.attribution, minZoom: 4, maxZoom: 12 } ).addTo( this.map );
}
// Put some points on the map
StoriesMap.prototype.populate = function populate( location ) {
var priority = location.zoom - 3;
if ( typeof points === "object" ) {
var buffer = [];
for ( var i in points ) {
var point = points[i];
if ( point.priority <= priority ) {
var circle = L.circle([ point.lat, point.lon ], 100000, {
color: '#f03', fillColor: '#f03', fillOpacity: 0.5
}).addTo( this.map );
}
}
}
}
// Filters map contents
StoriesMap.prototype.filter = function filter( map, location ) {
console.log( "filter" );
}
// Redraws map points
StoriesMap.prototype.redraw = function redraw( map, location ) {
console.log ( this.map )
for ( var i in map._layers ) {
if ( map._layers[i]._path != undefined ) {
try {
map.removeLayer( map._layers[i] );
}
catch(e) {
console.log("problem with " + e + map._layers[i]);
}
}
}
}
StoriesMap.prototype.fake = function fake() {
console.log("Always Called when object is Initialized");
}
当我运行时(从我的html):
var map = new Map();
我总是得到:
Always Called when object is Initialized
在我的控制台中。无论出于何种原因,我最后定义的原型被视为构造函数,我不确定为什么。我不希望在初始化对象时运行该函数,是否假定它是构造函数?这种情况以前发生在我身上,我的正常反应是创建一个名为“fake”的最后一个原型函数并将其留空,但这个问题的正确解决方案是什么?