JavaScript variables will store a reference to an object, but you can't share a reference to say a Number
or String
or undefined
. If marker
is anything but an array or object, then marker
and this.marker
will not be the same object when you modify it later (think of it as copying the value, instead of assigning the reference).
Instead try scoping the this
object, so later you can reference that object again:
function piano(numero, link) {
var self = this;
this.immagine = loadImage(link);
function createMarker() {
return new RichMarker({
//...
});
}
function loadImage(link) {
var immagine = new Image();
immagine.src = link;
immagine.onload = function() {
//use the scoped version of 'this'
self.marker = createMarker();
};
return immagine;
}
//...
}