Этот код не будет работать так, как вы ожидаете.
Ваш LatLongLines
является простым объектом, но this
только работает на instances
.
obj.method()
^----- DOT determines the value of this inside the called function
If obj is an instance it will be used as 'this' otherwise
'this' will default to the global 'window' object
Так что либо:
- Вы избавляетесь от всех
this
и вместо этого используете LatLongLines
, чтобы получить один глобальный объект
- Или вы создаете
LatLongLines
экземпляр, таким образом, this
будет работать, вы все равно можете выбрать синглтон (но вы знаете, что синглтон все еще является глобальным состоянием, а глобальное состояние - злом)
Если вам нужно создать новый экземпляр, сделайте что-то вроде этого:
function LatLongLines() {
this.Graticule = null;
this.Color = "#000000";
};
// Let's use prototypical inheritance, these functions are shared between all
// LatLongLines instances, of course the this always refers the specific instance that was called
LatLongLines.prototype = {
Show: function () {
this.Graticule.activate();
},
Hide; function () {
this.Graticule.deactivate()
},
Initialize: function () {
this.Graticule = new OpenLayers.Control.Graticule({
numPoints: 2,
labelled: true,
lineSymbolizer:{strokeColor: this.Color, strokeWidth: 2, strokeOpacity: 0.7},
labelSymbolizer:{strokeColor: this.Color, strokeWidth: 2, strokeOpacity: 0.7}
});
// Maybe you want to pass map in? :)
map.addControl(this.Graticule);
}
};
var someLines = new LatLongLines();