Node JS中全局变量可访问性的范围是什么?

时间:2020-02-01 11:51:53

标签: javascript node.js

我无法理解Node js中全局作用域的行为。在JS中,当我们在函数外部声明变量时,意味着在全局范围内可以在函数内部访问它并且可以对其进行突变。
但是在Node i中,我在函数外部声明了一个变量,可以在函数内部访问它,但该值未更改。

place=' Place One';
request({url:geoLocationAPI , json:true}, function(error , response){
if(error){
    console.log('Unable to connect.. !');
}
else if(response.message){
    console.log(response.message);
}
else
{
    const longt = response.body.features[0].center[0];
    const lat = response.body.features[0].center[1];
    // place = (response.body.features[0].place_name);
    console.log(place);   // output : Place One
    place = 'Place changed';
}
})
 console.log(place); // output : Place One

我认为它与callback有关。我已经了解了节点js中的执行上下文和事件循环。有人可以解释我的假设是否正确以及幕后发生的事情吗?

0 个答案:

没有答案