Skip to content

Call Stack & Memory heap [译] #13

@aicekiller

Description

@aicekiller

Call Stack

  • 存储函数的执行上下文
  • 遵循后进先出原则(LIFO)

Memory Heap

为变量和函数分配内存的地方。

看下面代码

console.log(1);
setTimeout(()=>{
   console.log(2);
});
console.log(3);
// 1,3,2

结合Stack和Heap分析上面代码是如何运行的:

  • console对象调用了log方法,将创建一个函数上下文,这个函数上下文将被推入函数上下文执行栈中;
  • 变量1将被保存在Memory Heap中;
  • console.log()不是异步调用,会直接执行,输出给定的参数;
  • 函数上下文会被从栈中推出。
  • setTimeout()函数被推入函数上下文执行栈;
  • 由于setTimeout()是异步函数,会稍后执行,setTimeout 函数将从堆栈中删除;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions