[[scope]]在JavaScript中是什么意思?

时间:2018-10-28 16:58:18

标签: javascript scope executioncontext

我已经通过Internet阅读了一些资源,大多数例子都与“带变量的范围”有关。所以我试图理解“功能范围”。

这是我的摘录。

debugger;

GetUserDetails((email) => {
debugger;
GetUsersGroups(email, (group) => {
    debugger;
    GetPosts(group, (posts) => {
        debugger;
        console.log(posts);
    });
});
});

function GetUserDetails(callback) {
debugger;
var email = 'xyz@gmail.com';
console.log(`Process started with email${email}!`);
callback(email);
}

function GetUsersGroups(email, callback) {
debugger;
// Todo: Api call
var groups = ['GroupUrl1', 'GroupUrl2', 'GroupUrl3', 'GroupUrl4'];
var groupsValue = JSON.stringify(groups);
console.log(`Got 4 groups: ${groupsValue} from email ${email}!`);
callback(groups[0]);
}

function GetPosts(groupUrl, callback) {
debugger;
 // Todo: Api call
var posts = ['Post1', 'Post2', 'Post3'];
var postsValue = JSON.stringify(posts);
console.log(`Got 3 posts: ${postsValue} from group ${groupUrl}!`);
callback(postsValue);
}

在将全局执行上下文放入执行堆栈时,所有回调函数都添加到范围中。

[https://screencast-o-matic.com/watch/cF6uDMYEbN]

为什么会发生?

参考:https://medium.freecodecamp.org/deep-dive-into-scope-chains-and-closures-21ee18b71dd9

谢谢!

0 个答案:

没有答案