如何从函数范围外访问数据?

时间:2017-06-16 18:23:38

标签: javascript twitter

我是一名试图学习基础知识的新编码员。为了练习,我构建了一个小的Node应用程序,用于从用户的时间线中获取最后一条推文并将其反转。这是代码:

// REQUIRES
var dotenv = require('dotenv');
var Twitter = require('twitter');

// LOAD ENV VARIABLES
dotenv.load();

// MAKE A TWITTER CLIENT
var client = new Twitter({
  consumer_key: process.env.TWITTER_CONSUMER_KEY,
  consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
  access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
  access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
});

// FUNCTION TO REVERSE A STRING
function reverse(s) {
  var o = '';
  for (var i = s.length - 1; i >= 0; i--)
    o += s[i];
  return o;
}

// GRAB LAST TWEET FROM USER TIMELINE AND REVERSE IT
client.get('statuses/user_timeline', {screen_name: 'twitterapi', count: 1}, function(error, tweets, response) {
  if(error) throw error;
  // console.log(tweets);  // Last tweet object from timeline.
  var lastTweet = tweets[0].text; // Text from last tweet.
  var reverseTweet = reverse(lastTweet); // Last tweet reversed.
  console.log("\nYour last tweet was: " + lastTweet);
  console.log("\nYour last tweet reversed is: " + reverseTweet);
});

我的问题是:如果我想将lastTweetreverseTweet传递给client.get()范围之外的另一个函数(例如,传递给另一个文本解析函数),我该怎么办?那样做?

我已经尝试过全局声明变量,但我认为请求/响应的异步性质将其搞砸了。我肯定愿意学习承诺,回调等等(我现在理解zilch)但我不确定这是否是解决这一特定问题的正确方法。

最后:如果我没有使用正确的词汇来提出正确的问题,请告诉我。

2 个答案:

答案 0 :(得分:1)

您可以在更高的范围级别声明变量:

// REQUIRES
var dotenv = require('dotenv');
var Twitter = require('twitter');
var lastTweet = null;
var reverseTweet = null;

// LOAD ENV VARIABLES
dotenv.load();

...

或者考虑到client.get是异步调用这一事实的最好方法是将lastTweetreverseTweet作为参数传递给另一个函数并在回调中调用该函数client.get。例如:

// REQUIRES
var dotenv = require('dotenv');
var Twitter = require('twitter');

// LOAD ENV VARIABLES
dotenv.load();

// MAKE A TWITTER CLIENT
var client = new Twitter({
  consumer_key: process.env.TWITTER_CONSUMER_KEY,
  consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
  access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
  access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
});

// FUNCTION TO REVERSE A STRING
function reverse(s) {
  var o = '';
  for (var i = s.length - 1; i >= 0; i--)
    o += s[i];
  return o;
}

// GRAB LAST TWEET FROM USER TIMELINE AND REVERSE IT
client.get('statuses/user_timeline', {screen_name: 'twitterapi', count: 1}, function(error, tweets, response) {
  if(error) throw error;
  // console.log(tweets);  // Last tweet object from timeline.
  var lastTweet = tweets[0].text; // Text from last tweet.
  var reverseTweet = reverse(lastTweet); // Last tweet reversed.
  console.log("\nYour last tweet was: " + lastTweet);
  console.log("\nYour last tweet reversed is: " + reverseTweet);

  anotherFunction(lastTweet, reverseTweet);
});

function anotherFunction(lastTweet, reverseTweet)
{
    // Do something with vars here
}

答案 1 :(得分:0)

您必须在client.get函数之外声明reverseTweet或lastTweet。

你可以这样做:

// FUNCTION TO REVERSE A STRING
function reverse(s) {
  var o = '';
  for (var i = s.length - 1; i >= 0; i--)
    o += s[i];
  return o;
}
var lastTweet = "";
var reverseTweet = "";

// GRAB LAST TWEET FROM USER TIMELINE AND REVERSE IT
client.get('statuses/user_timeline', {screen_name: 'twitterapi', count:     1}, function(error, tweets, response) {
  if(error) throw error;
  // console.log(tweets);  // Last tweet object from timeline.
  lastTweet = tweets[0].text; // Text from last tweet.
  reverseTweet = reverse(lastTweet); // Last tweet reversed.
  console.log("\nYour last tweet was: " + lastTweet);
  console.log("\nYour last tweet reversed is: " + reverseTweet);
});