Javascript 在函数外使用变量

Javascript 在函数外使用变量,javascript,node.js,Javascript,Node.js,首先,我要说的是,我确实在努力学习node,但遇到了一些困难。我正在构建一个超级简单的天气应用程序,调用API,然后我想返回数据 但我不知道为什么它不会将这些数据返回到控制台 var request = require('request'); request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) { if (!error &

首先,我要说的是,我确实在努力学习node,但遇到了一些困难。我正在构建一个超级简单的天气应用程序,调用API,然后我想返回数据

但我不知道为什么它不会将这些数据返回到控制台

var request = require('request');

request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      //var info = JSON.parse(body);
      var jsonObject = JSON.parse(body);
      var summary = jsonObject.currently.summary;
      var temp = jsonObject.currently.temperature;
      var realFeel = jsonObject.currently.apparentTemperature;
      var summary2 = jsonObject.hourly.summary;
      var max = jsonObject.daily.data[0].temperatureMax;
      var min = jsonObject.daily.data[0].temperatureMin;
    }
})
console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);

所有变量都是回调函数的局部变量。将console.log语句移动到函数中

var request = require('request');

request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      //var info = JSON.parse(body);
      var jsonObject = JSON.parse(body);
      var summary = jsonObject.currently.summary;
      var temp = jsonObject.currently.temperature;
      var realFeel = jsonObject.currently.apparentTemperature;
      var summary2 = jsonObject.hourly.summary;
      var max = jsonObject.daily.data[0].temperatureMax;
      var min = jsonObject.daily.data[0].temperatureMin;
      console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
    }
})
如果希望所有变量都是全局变量(坏主意),请从回调函数中删除
var
语句:

(function (){ // add this line
  var request = require('request');
  var summary, temp, realFeel, summary2, max, min;

  request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      //var info = JSON.parse(body);
      var jsonObject = JSON.parse(body);
      summary = jsonObject.currently.summary;
      temp = jsonObject.currently.temperature;
      realFeel = jsonObject.currently.apparentTemperature;
      summary2 = jsonObject.hourly.summary;
      max = jsonObject.daily.data[0].temperatureMax;
      min = jsonObject.daily.data[0].temperatureMin;

      console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
    }
  });
}()); // and this line to avoid global variables
但是你仍然需要一段代码来显示你的天气摘要。post中回调函数外部的console.log将在数据从forecast服务返回之前运行。简而言之,必须将console.log放在回调函数中

var request = require('request');

request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      //var info = JSON.parse(body);
      var jsonObject = JSON.parse(body);
      var summary = jsonObject.currently.summary;
      var temp = jsonObject.currently.temperature;
      var realFeel = jsonObject.currently.apparentTemperature;
      var summary2 = jsonObject.hourly.summary;
      var max = jsonObject.daily.data[0].temperatureMax;
      var min = jsonObject.daily.data[0].temperatureMin;
      console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
    }
})

这个答案直接回答了你的具体问题。请理解创建。

所有变量都是回调函数的本地变量。将console.log语句移动到函数中

var request = require('request');

request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      //var info = JSON.parse(body);
      var jsonObject = JSON.parse(body);
      var summary = jsonObject.currently.summary;
      var temp = jsonObject.currently.temperature;
      var realFeel = jsonObject.currently.apparentTemperature;
      var summary2 = jsonObject.hourly.summary;
      var max = jsonObject.daily.data[0].temperatureMax;
      var min = jsonObject.daily.data[0].temperatureMin;
      console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
    }
})
如果希望所有变量都是全局变量(坏主意),请从回调函数中删除
var
语句:

(function (){ // add this line
  var request = require('request');
  var summary, temp, realFeel, summary2, max, min;

  request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      //var info = JSON.parse(body);
      var jsonObject = JSON.parse(body);
      summary = jsonObject.currently.summary;
      temp = jsonObject.currently.temperature;
      realFeel = jsonObject.currently.apparentTemperature;
      summary2 = jsonObject.hourly.summary;
      max = jsonObject.daily.data[0].temperatureMax;
      min = jsonObject.daily.data[0].temperatureMin;

      console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
    }
  });
}()); // and this line to avoid global variables
但是你仍然需要一段代码来显示你的天气摘要。post中回调函数外部的console.log将在数据从forecast服务返回之前运行。简而言之,必须将console.log放在回调函数中

var request = require('request');

request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      //var info = JSON.parse(body);
      var jsonObject = JSON.parse(body);
      var summary = jsonObject.currently.summary;
      var temp = jsonObject.currently.temperature;
      var realFeel = jsonObject.currently.apparentTemperature;
      var summary2 = jsonObject.hourly.summary;
      var max = jsonObject.daily.data[0].temperatureMax;
      var min = jsonObject.daily.data[0].temperatureMin;
      console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
    }
})

这个答案直接回答了你的具体问题。请理解创建。

所有变量都是回调函数的本地变量。将console.log语句移动到函数中

var request = require('request');

request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      //var info = JSON.parse(body);
      var jsonObject = JSON.parse(body);
      var summary = jsonObject.currently.summary;
      var temp = jsonObject.currently.temperature;
      var realFeel = jsonObject.currently.apparentTemperature;
      var summary2 = jsonObject.hourly.summary;
      var max = jsonObject.daily.data[0].temperatureMax;
      var min = jsonObject.daily.data[0].temperatureMin;
      console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
    }
})
如果希望所有变量都是全局变量(坏主意),请从回调函数中删除
var
语句:

(function (){ // add this line
  var request = require('request');
  var summary, temp, realFeel, summary2, max, min;

  request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      //var info = JSON.parse(body);
      var jsonObject = JSON.parse(body);
      summary = jsonObject.currently.summary;
      temp = jsonObject.currently.temperature;
      realFeel = jsonObject.currently.apparentTemperature;
      summary2 = jsonObject.hourly.summary;
      max = jsonObject.daily.data[0].temperatureMax;
      min = jsonObject.daily.data[0].temperatureMin;

      console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
    }
  });
}()); // and this line to avoid global variables
但是你仍然需要一段代码来显示你的天气摘要。post中回调函数外部的console.log将在数据从forecast服务返回之前运行。简而言之,必须将console.log放在回调函数中

var request = require('request');

request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      //var info = JSON.parse(body);
      var jsonObject = JSON.parse(body);
      var summary = jsonObject.currently.summary;
      var temp = jsonObject.currently.temperature;
      var realFeel = jsonObject.currently.apparentTemperature;
      var summary2 = jsonObject.hourly.summary;
      var max = jsonObject.daily.data[0].temperatureMax;
      var min = jsonObject.daily.data[0].temperatureMin;
      console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
    }
})

这个答案直接回答了你的具体问题。请理解创建。

所有变量都是回调函数的本地变量。将console.log语句移动到函数中

var request = require('request');

request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      //var info = JSON.parse(body);
      var jsonObject = JSON.parse(body);
      var summary = jsonObject.currently.summary;
      var temp = jsonObject.currently.temperature;
      var realFeel = jsonObject.currently.apparentTemperature;
      var summary2 = jsonObject.hourly.summary;
      var max = jsonObject.daily.data[0].temperatureMax;
      var min = jsonObject.daily.data[0].temperatureMin;
      console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
    }
})
如果希望所有变量都是全局变量(坏主意),请从回调函数中删除
var
语句:

(function (){ // add this line
  var request = require('request');
  var summary, temp, realFeel, summary2, max, min;

  request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      //var info = JSON.parse(body);
      var jsonObject = JSON.parse(body);
      summary = jsonObject.currently.summary;
      temp = jsonObject.currently.temperature;
      realFeel = jsonObject.currently.apparentTemperature;
      summary2 = jsonObject.hourly.summary;
      max = jsonObject.daily.data[0].temperatureMax;
      min = jsonObject.daily.data[0].temperatureMin;

      console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
    }
  });
}()); // and this line to avoid global variables
但是你仍然需要一段代码来显示你的天气摘要。post中回调函数外部的console.log将在数据从forecast服务返回之前运行。简而言之,必须将console.log放在回调函数中

var request = require('request');

request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      //var info = JSON.parse(body);
      var jsonObject = JSON.parse(body);
      var summary = jsonObject.currently.summary;
      var temp = jsonObject.currently.temperature;
      var realFeel = jsonObject.currently.apparentTemperature;
      var summary2 = jsonObject.hourly.summary;
      var max = jsonObject.daily.data[0].temperatureMax;
      var min = jsonObject.daily.data[0].temperatureMin;
      console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
    }
})

这个答案直接回答了你的具体问题。请理解创建。

节点是一个异步服务器。当您执行要阻止的请求时,节点引擎将继续执行以下代码。您正在向请求模块传递回调,但您的console.log会在请求被触发后立即执行,然后返回结果

您要做的是将代码放在回调函数中使用数据的位置

var request = require('request');

request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      //var info = JSON.parse(body);
      var jsonObject = JSON.parse(body);
      var summary = jsonObject.currently.summary;
      var temp = jsonObject.currently.temperature;
      var realFeel = jsonObject.currently.apparentTemperature;
      var summary2 = jsonObject.hourly.summary;
      var max = jsonObject.daily.data[0].temperatureMax;
      var min = jsonObject.daily.data[0].temperatureMin;
      console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
    }
})
您也不应该忽略错误

var request = require('request');

request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (error) throw error;
    if (response.statusCode != 200) return console.log('Invalid status code: '+response.statusCode)                     
    var jsonObject = JSON.parse(body);
    var summary = jsonObject.currently.summary;
    var temp = jsonObject.currently.temperature;
    var realFeel = jsonObject.currently.apparentTemperature;
    var summary2 = jsonObject.hourly.summary;
    var max = jsonObject.daily.data[0].temperatureMax;
    var min = jsonObject.daily.data[0].temperatureMin;
    console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
});

节点是一个异步服务器。当您执行要阻止的请求时,节点引擎将继续执行以下代码。您正在向请求模块传递回调,但您的console.log会在请求被触发后立即执行,然后返回结果

您要做的是将代码放在回调函数中使用数据的位置

var request = require('request');

request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      //var info = JSON.parse(body);
      var jsonObject = JSON.parse(body);
      var summary = jsonObject.currently.summary;
      var temp = jsonObject.currently.temperature;
      var realFeel = jsonObject.currently.apparentTemperature;
      var summary2 = jsonObject.hourly.summary;
      var max = jsonObject.daily.data[0].temperatureMax;
      var min = jsonObject.daily.data[0].temperatureMin;
      console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
    }
})
您也不应该忽略错误

var request = require('request');

request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (error) throw error;
    if (response.statusCode != 200) return console.log('Invalid status code: '+response.statusCode)                     
    var jsonObject = JSON.parse(body);
    var summary = jsonObject.currently.summary;
    var temp = jsonObject.currently.temperature;
    var realFeel = jsonObject.currently.apparentTemperature;
    var summary2 = jsonObject.hourly.summary;
    var max = jsonObject.daily.data[0].temperatureMax;
    var min = jsonObject.daily.data[0].temperatureMin;
    console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
});

节点是一个异步服务器。当您执行要阻止的请求时,节点引擎将继续执行以下代码。您正在向请求模块传递回调,但您的console.log会在请求被触发后立即执行,然后返回结果

您要做的是将代码放在回调函数中使用数据的位置

var request = require('request');

request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      //var info = JSON.parse(body);
      var jsonObject = JSON.parse(body);
      var summary = jsonObject.currently.summary;
      var temp = jsonObject.currently.temperature;
      var realFeel = jsonObject.currently.apparentTemperature;
      var summary2 = jsonObject.hourly.summary;
      var max = jsonObject.daily.data[0].temperatureMax;
      var min = jsonObject.daily.data[0].temperatureMin;
      console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
    }
})
您也不应该忽略错误

var request = require('request');

request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (error) throw error;
    if (response.statusCode != 200) return console.log('Invalid status code: '+response.statusCode)                     
    var jsonObject = JSON.parse(body);
    var summary = jsonObject.currently.summary;
    var temp = jsonObject.currently.temperature;
    var realFeel = jsonObject.currently.apparentTemperature;
    var summary2 = jsonObject.hourly.summary;
    var max = jsonObject.daily.data[0].temperatureMax;
    var min = jsonObject.daily.data[0].temperatureMin;
    console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
});

节点是一个异步服务器。当您执行要阻止的请求时,节点引擎将继续执行以下代码。您正在向请求模块传递回调,但您的console.log会在请求被触发后立即执行,然后返回结果

您要做的是将代码放在回调函数中使用数据的位置

var request = require('request');

request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      //var info = JSON.parse(body);
      var jsonObject = JSON.parse(body);
      var summary = jsonObject.currently.summary;
      var temp = jsonObject.currently.temperature;
      var realFeel = jsonObject.currently.apparentTemperature;
      var summary2 = jsonObject.hourly.summary;
      var max = jsonObject.daily.data[0].temperatureMax;
      var min = jsonObject.daily.data[0].temperatureMin;
      console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
    }
})
您也不应该忽略错误

var request = require('request');

request('https://api.forecast.io/forecast/{API KEY}/42.47994,-83.13040', function (error, response, body) {
    if (error) throw error;
    if (response.statusCode != 200) return console.log('Invalid status code: '+response.statusCode)                     
    var jsonObject = JSON.parse(body);
    var summary = jsonObject.currently.summary;
    var temp = jsonObject.currently.temperature;
    var realFeel = jsonObject.currently.apparentTemperature;
    var summary2 = jsonObject.hourly.summary;
    var max = jsonObject.daily.data[0].temperatureMax;
    var min = jsonObject.daily.data[0].temperatureMin;
    console.log('Todays forecast ' + summary2 + ' With a current Tempature of ' + temp + '. But feels like ' +realFeel + ' With a high of '+max+' and a low of '+min);
});

问题是,当您使用
var
声明变量时,您将其范围声明为您所在的函数。如果您使用的名称没有使用var声明,它将在全局“窗口”范围中查找它们


在回调函数中删除var关键字,该函数应该改变全局范围

问题在于,当您使用
var
声明变量时,您将其范围声明为您所在的函数。如果您使用的名称没有使用var声明,它将在全局“窗口”范围中查找它们


在回调函数中删除var关键字,该函数应该改变全局范围

问题在于,当您使用
var
声明变量时,您将其范围声明为您所在的函数。如果您使用的名称没有使用var声明,它将在全局“窗口”范围中查找它们


在回调函数中删除var关键字,该函数应该改变全局范围

问题在于,当您使用
var
声明变量时,您将其范围声明为您所在的函数。如果您使用的名称没有使用var声明,它将在全局“窗口”范围中查找它们

在回调函数中删除var关键字,该函数应该改变globa