How To Use A Variable In A Different Function?
Im trying to print out the latitude (lat) variable in the javascript below. How do I refer to the lat variable? this.lat, that.lat, vicinity.lat etc ? In my javascript I have var V
Solution 1:
You're nearly there. You already are declaring that
to be this
in init()
. Just do that throughout the function and it should work:
varVicinity = function (pubReference, sessionId, done, err) {
var that = this;
this.ad = {
getBannerHtml: function () {
console.log(that.lat); // how do I refer to the lat variable?
}
};
this.init = function (done, err) {
var that = this;
this.getLatLng(that, function (position) {
that.lat = position.coords.latitude;
}, err);
if (typeof done === 'function')
done(this);
};
this.init(done, err);
};
$(document).ready(function () {
var data = newVicinity(pubReference, sessionId,
function (result) {
$("#sticky").html(result.ad.getBannerHtml());
}
);
});
Solution 2:
You can also call the getBannerHtml method in the scope of your Vicinity by using the "apply" function. The "this" variable is set to the object you pass.
$(document).ready(function () {
var data = newVicinity(pubReference, sessionId,
function (result) {
$("#sticky").html(result.ad.getBannerHtml.apply(result));
}
);
});
Solution 3:
I already posted in another post but got another idea ;) It is kind of obvious but works.
getBannerHtml: function(lat, lon) {
return'<iframe src="http://foo.com?pr=34&lat'+ lat +'=&lon=' + lon + '"></iframe>';
}
$(document).ready(function() {
var pubReference = 1;
var sessionId = "1";
var data = newVicinity (pubReference, sessionId,
function(result) {
$("#sticky").html(result.ad.getBannerHtml(result.lat, result.lon));
}
);
});
You can also call the getBannerHtml method in the scope of your Vicinity by using the "apply" function. The "this" variable is set to the object you pass.
$(document).ready(function () {
var pubReference = 1;
var sessionId = "1";
var data = newVicinity(pubReference, sessionId,
function (result) {
$("#sticky").html(result.ad.getBannerHtml.apply(result));
}
);
});
Post a Comment for "How To Use A Variable In A Different Function?"