Javascript中的作用域/闭包-如何更新全局变量?
所以我一直对此感到困惑,只是不知道如何解决这个问题 我有一个嵌套函数,它循环遍历一系列对象,并获取每个对象URL的社交链接 之后,我想通过包含Javascript中的作用域/闭包-如何更新全局变量?,javascript,function,nested,closures,Javascript,Function,Nested,Closures,所以我一直对此感到困惑,只是不知道如何解决这个问题 我有一个嵌套函数,它循环遍历一系列对象,并获取每个对象URL的社交链接 之后,我想通过包含{social:[array of social url]}来更新每个对象。但是,我得到的是{social:[]} 我已经尝试将联系人[I].social=results放在这篇文章的其他部分,但出现了“未找到对象”错误 非常感谢您的帮助 var contacts = [ { title: 'Healthy breakfast: Quick, flex
{social:[array of social url]}
来更新每个对象。但是,我得到的是{social:[]}
我已经尝试将联系人[I].social=results放在这篇文章的其他部分,但出现了“未找到对象”错误
非常感谢您的帮助
var contacts = [
{ title: 'Healthy breakfast: Quick, flexible options to grab at home - Mayo Clinic',
url: 'http://www.mayoclinic.org/healthy-living/nutrition-and-healthy-eating/in-depth/food-and-nutrition/art-20048294' },
{ title: 'Healthy Breakfast Ideas from Dr. Weil\'s Facebook Readers',
url: 'http://www.drweil.com/drw/u/ART03113/Healthy-Breakfast-Ideas-from-Facebook-Readers.html' },
{ title: '8 Healthy Breakfast Ideas - Prevention.com',
url: 'http://www.prevention.com/food/healthy-eating-tips/8-healthy-breakfast-ideas' },
{ title: 'Quick & Easy Healthy Breakfast Ideas! - YouTube',
url: 'http://www.youtube.com/watch?v=mD4YSD8LDiQ' },
{ title: 'The Best Foods to Eat for Breakfast - Health.com',
url: 'http://www.health.com/health/gallery/0,,20676415,00.html' },
{ title: 'Healthy Breakfast Recipes - Secrets To Cooking Healthier - YouTube',
url: 'http://www.youtube.com/watch?v=7jH0xe1XKxI' },
{ title: 'Healthy Breakfast Ideas You Can Make the Night Before - FitSugar',
url: 'http://www.fitsugar.com/Healthy-Breakfast-Ideas-You-Can-Make-Night-Before-20048633' },
{ title: '10 Easy, 5-Minute Breakfast Ideas - Diet and ... - Everyday Health',
url: 'http://www.everydayhealth.com/diet-and-nutrition-pictures/easy-5-minute-breakfast-ideas.aspx' },
{ title: 'Healthy Breakfast Ideas for Kids | Parenting - Parenting.com',
url: 'http://www.parenting.com/gallery/healthy-breakfast-ideas-kids' },
{ title: 'Fruits & Veggies More Matters : Healthy Breakfast Ideas : Health ...',
url: 'http://www.fruitsandveggiesmorematters.org/healthy-breakfast-ideas' }
];
scraper(contacts);
// loops through contacts database and scrapes requested information
function scraper(contacts) {
// Adds the domain of each contact
for(var i=0;i<contacts.length;i++){
contacts[i].domain = contacts[i].url.split(/\//, 3).join().replace(/,/g, '/');
};
//
for(var i=0;i<contacts.length;i++){
var homepage = contacts[i].domain;
var results = [];
function socialScrape(homepage) {
request(homepage, function(err, resp, html) {
var $ = cheerio.load(html);
if(!err && resp.statusCode == 200) {
$('a').each(function(i, el){
var a = $(el).attr('href');
for(var key in socialURLS){
if(socialURLS[key].test(a) && results.indexOf(a) < 0){
results.push(a);
}
}
});
} else { console.log(err); }
})
}
contacts[i].social = results;
socialScrape(homepage);
}
console.log(contacts);
}
var联系人=[
{标题:“健康早餐:在家里快速、灵活的选择——梅奥诊所”,
网址:'http://www.mayoclinic.org/healthy-living/nutrition-and-healthy-eating/in-depth/food-and-nutrition/art-20048294' },
{标题:'威尔博士的Facebook读者提供的健康早餐想法',
网址:'http://www.drweil.com/drw/u/ART03113/Healthy-Breakfast-Ideas-from-Facebook-Readers.html' },
{标题:'8个健康早餐理念-预防.com',
网址:'http://www.prevention.com/food/healthy-eating-tips/8-healthy-breakfast-ideas' },
{标题:'快速简单的健康早餐想法!-YouTube',
网址:'http://www.youtube.com/watch?v=mD4YSD8LDiQ' },
{标题:“早餐吃的最好的食物-Health.com”,
网址:'http://www.health.com/health/gallery/0,20676415,00.html'},
{标题:“健康早餐食谱-烹饪更健康的秘密-YouTube”,
网址:'http://www.youtube.com/watch?v=7jH0xe1XKxI' },
{标题:'你可以在前一天晚上做的健康早餐-FitSugar',
网址:'http://www.fitsugar.com/Healthy-Breakfast-Ideas-You-Can-Make-Night-Before-20048633' },
{标题:'10个简单的,5分钟的早餐想法-饮食和…-日常健康',
网址:'http://www.everydayhealth.com/diet-and-nutrition-pictures/easy-5-minute-breakfast-ideas.aspx' },
{标题:“儿童健康早餐理念|亲职-亲职网”,
网址:'http://www.parenting.com/gallery/healthy-breakfast-ideas-kids' },
{标题:'水果和蔬菜更多事项:健康早餐理念:健康…',
网址:'http://www.fruitsandveggiesmorematters.org/healthy-breakfast-ideas' }
];
刮刀(触点);
//循环浏览联系人数据库并获取请求的信息
功能刮刀(触点){
//添加每个联系人的域
对于(var i=0;i您的第一个问题是您的请求
调用是异步的,在contacts[i]执行时尚未返回。social=results
被执行,因此contacts[i]。results
被分配一个空数组[]。(这个问题的变体每天都会被发布很多次,在这里可以找到关于这个问题的一个很好的解释:)解决这个问题的方法并不像移动联系人[i]那么简单.social=results;
进入请求调用成功处理程序的内部,因为i
的值在调用处理程序之前已更改
您的第二个问题是,结果
是在社会垃圾
函数定义之外定义的-因此,不是每个请求
调用都有一个项目数组,而是一个包含所有请求
结果的数组。解决范围界定问题的最佳方法是使用闭包,我们可以通过删除他调用socialScrape(主页);
并使socialScrape
成为一个自调用函数:
(function socialScrape(homepage) {
var results = [];
var index = i;
request(homepage, function(err, resp, html) {
/* do error and status check stuff and build our results array */
contacts[index].social = results;
});
}(homepage));
请注意,我们如何在闭包中捕获i
的当前值,并将其分配给index
。这将允许我们在交付结果时按索引获得正确的联系人