简体   繁体   English

Javascript(变量范围):变量未获取分配给它的值

[英]Javascript (variable scopes): variable does not get the value assigned to it

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. 我是JS的新手,正在尝试在函数中设置变量的值,但是在设置值后它是“未定义的”。 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. 我希望IP变量在函数内部和外部具有相同的值,但事实并非如此。 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? 从类似的问题123 ,我没有使用VAR重新声明在函数中一个局部变量,而我使用指向IP变量函数外“这个”,所以我不明白为什么它不能按预期工作?

No need to use this.IP , when you use this inside the API call, you are referring to that API call function itself. 无需使用this.IP ,当您在API调用中使用this时, this.IP该API调用函数本身。 Just using IP should work. 仅使用IP应该可以。

The alert(IP); alert(IP); outside the API call wouldn't return the result as well. 在API调用之外也不会返回结果。 This function call will be immediately called even before the API returns some data so it will print out the previous value undefined . 即使在API返回某些数据之前,也会立即调用此函数调用,因此它将打印出先前undefined值。

(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 = undefined;                                                               
    $.getJSON('http://gd.geobytes.com/GetCityDetails?callback=?', function(data) {
     IP = JSON.stringify(data, null, 2);
     alert(IP);
    });

    alert(IP); //This will return undefined
});

If you want get IP value at anywhere, use follow method: 如果要在任何地方获取IP值,请使用以下方法:

var IP = undefined;
var fpData = {};
function setIP(){
  var valueIP = undefined;
  $.ajax({
        url: "http://gd.geobytes.com/GetCityDetails?callback=?",
        async: false,
        dataType: 'json',
        success: function(data) {
            valueIP = JSON.stringify(data, null, 2); //console.log(valueIP);
            //return valueIP;
            localStorage.setItem('IP', valueIP);
        }
    });
  //return valueIP;
}

//call setIP
setIP();
IP = localStorage.getItem('IP');
console.log(IP);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM