I'm very new to JS and I'm trying to set the value of a variable within a function but it is 'undefined' after setting the value. My code is as follows:
(function (name, context, definition){ 'use strict' ...} ('FP2', this, function (){...})
var fp = new FP2();
fp.get(function(result, components) { for (var index in components){...};
var IP = 'nothing';
$.getJSON('http://gd.geobytes.com/GetCityDetails?callback=?', function(data) {
this.IP = JSON.stringify(data, null, 2);
alert(this.IP);
});
alert(IP);
});
I expect that the IP variable have the same value inside and outside the function, but it does not. The inner alert shows the expected value but the alert outside the function shows 'nothing'. From similar questions 1, 2, 3, I'm not using var to redeclare a local variable inside the function, and I'm pointing to the IP variable outside the function using 'this.', so I don't understand why it does not work as expected?