当前位置: 首页 > 编程日记 > 正文

javascript 堆栈_JavaScript调用堆栈-它是什么以及为什么它是必需的

javascript 堆栈

The JavaScript engine (which is found in a hosting environment like the browser), is a single-threaded interpreter comprising of a heap and a single call stack. The browser provides web APIs like the DOM, AJAX, and Timers.

JavaScript引擎(可在诸如浏览器之类的托管环境中找到)是一个单线程解释器,由堆和单个调用堆栈组成。 浏览器提供了DOM,AJAX和Timers等Web API。

This article is aimed at explaining what the call stack is and why it is needed. An understanding of the call stack will give clarity to how “function hierarchy and execution order” works in the JavaScript engine.

本文旨在说明什么是调用堆栈以及为什么需要调用堆栈。 对调用栈的理解将使“函数层次结构和执行顺序”在JavaScript引擎中的工作方式更加清晰。

The call stack is primarily used for function invocation (call). Since the call stack is single, function(s) execution, is done, one at a time, from top to bottom. It means the call stack is synchronous.

调用堆栈主要用于函数调用(调用)。 由于调用堆栈是单个的,因此函数的执行从上到下一次完成。 这意味着调用堆栈是同步的。

The understanding of the call stack is vital to Asynchronous programming (which we will look at in a later article).

对调用栈的理解对于异步编程至关重要(我们将在以后的文章中介绍)。

In Asynchronous JavaScript, we have a callback function, an event loop, and a task queue. The callback function is acted upon by the call stack during execution after the call back function has been pushed to the stack by the event loop.

在异步JavaScript中,我们有一个回调函数,一个事件循环和一个任务队列。 在事件循环将回调函数推到堆栈之后,回调函数将在执行期间由调用堆栈执行。

But before we jump the gun, let us first attempt to answer the question - What is the call stack?

但是,在我们开枪之前,让我们首先尝试回答问题-调用堆栈是什么?

At the most basic level, a call stack is a data structure that uses the Last In, First Out (LIFO) principle to temporarily store and manage function invocation (call).

在最基本的级别上,调用堆栈是一种数据结构,它使用后进先出(LIFO)原理临时存储和管理函数调用(调用)。

Let’s break down our definition:

让我们打破我们的定义:

LIFO: When we say that the call stack, operates by the data structure principle of Last In, First Out, it means that the last function that gets pushed into the stack is the first to be pop out, when the function returns.

LIFO:当我们说调用堆栈是按照后进先出的数据结构原理进行操作时,这意味着当函数返回时,被压入堆栈的最后一个函数是第一个弹出的函数。

Let us take a look at a code sample to demonstrate LIFO by printing a stack trace error to the console.

让我们看一下通过将堆栈跟踪错误打印到控制台来演示LIFO的代码示例。

function firstFunction(){throw new Error('Stack Trace Error');
}function secondFunction(){firstFunction();
}function thirdFunction(){secondFunction();
}thirdFunction();

When the code is run, we get an error. A stack is printed showing how the functions are stack on top each other. Take a look at the diagram.

运行代码时,我们会收到错误消息。 会打印出一个堆栈,说明如何将功能堆叠在一起。 看一下该图。

You will notice that the arrangement of the functions as a stack begins with the firstFunction() (which is the last function that got into the stack, and is popped out to throw the error), followed by the secondFunction(), and then the thirdFunction() (which is the first function that gets pushed into the stack when the code is executed).

您会注意到,函数作为堆栈的排列以firstFunction() (它是进入堆栈的最后一个函数,然后弹出弹出错误)的位置开始,然后是secondFunction() ,然后是thirdFunction() (这是在执行代码时被压入堆栈的第一个函数)。

Temporarily store: When a function is invoked (called), the function, its parameters, and variables are pushed into the call stack to form a stack frame. This stack frame is a memory location in the stack. The memory is cleared when the function returns as it is pop out of the stack.

临时存储 :调用(调用)一个函数时,该函数,其参数和变量被推入调用堆栈以形成堆栈框架。 该堆栈帧是堆栈中的内存位置。 当函数返回时(从栈中弹出),将清除内存。

Manage function invocation (call): The call stack maintains a record of the position of each stack frame. It knows the next function to be executed (and will remove it after execution). This is what makes code execution in JavaScript synchronous.

管理功能调用(调用) :调用堆栈维护每个堆栈帧位置的记录。 它知道下一个要执行的功能(并在执行后将其删除)。 这就是使JavaScript中的代码执行同步的原因。

Think of yourself standing on a queue, in a grocery store cash point. You can only be attended to after the person in front of you have been attended to. That’s synchronous.

想想自己站在杂货店现金点的队列中。 只有当您面前的人受到照顾后,您才能得到照顾。 那是同步的。

This is what we mean by “manage function invocation”.

这就是我们所说的“管理功能调用”。

调用堆栈如何处理函数调用? (How does the call stack handle function calls?)

We will answer this question by looking at a sample code of a function that calls another function. Here is the example code:

我们将通过查看调用另一个函数的函数的示例代码来回答这个问题。 这是示例代码:

function firstFunction(){console.log("Hello from firstFunction");
}function secondFunction(){firstFunction();console.log("The end from secondFunction");
}secondFunction();

This is what happens when the code is run:

这是运行代码时发生的情况:

1. When secondFunction() gets executed, an empty stack frame is created. It is the main (anonymous) entry point of the program.2. secondFunction() then calls firstFunction()which is pushed into the stack.3. firstFunction() returns and prints “Hello from firstFunction” to the console.4. firstFunction() is pop off the stack.5. The execution order then move to secondFunction().6. secondFunction() returns and print “The end from secondFunction” to the console.7. secondFunction() is pop off the stack, clearing the memory.

1.执行secondFunction() ,将创建一个空的堆栈框架。 它是程序的主要(匿名)入口点。 secondFunction()然后调用firstFunction() ,该函数被压入firstFunction()firstFunction()返回并将“ Hello from firstFunction”打印到控制台。4。 firstFunction()从堆栈中弹出5。 然后,执行顺序移至secondFunction() .6。 secondFunction()返回并将“ secondFunction的结尾”打印到控制台。7。 secondFunction()从堆栈中弹出,清除内存。

是什么导致堆栈溢出? (What causes a stack overflow?)

A stack overflow occurs when there is a recursive function (a function that calls itself) without an exit point. The browser (hosting environment) has a maximum stack call that it can accomodate before throwing a stack error.

当存在没有出口点的递归函数(调用自身的函数)时,将发生堆栈溢出。 浏览器(宿主环境)具有最大的堆栈调用,在引发堆栈错误之前,它可以适应。

Here is an example:

这是一个例子:

function callMyself(){callMyself();
}callMyself();

The callMyself() will run until the browser throws a “Maximum call size exceeded”. And that is a stack overflow.

callMyself()将一直运行,直到浏览器抛出“超出最大呼叫大小”为止。 那就是堆栈溢出。

综上所述 (In summary)

The key takeaways from the call stack are:1. It is single-threaded. Meaning it can only do one thing at a time.2. Code execution is synchronous.3. A function invocation creates a stack frame that occupies a temporary memory.4. It works as a LIFO — Last In, First Out data structure.

调用堆栈的主要收获是:1.。 它是单线程的。 一次只能做一件事2。 代码执行是同步的3。 函数调用会创建一个占用临时内存的堆栈帧4。 它用作LIFO —后进先出数据结构。

We have used the call stack article to lay the foundation for a series we will be looking at on Asynchronous JavaScript (which we will be looking at in another article).

我们已经使用了调用栈文章为我们将在异步JavaScript上进行学习的系列奠定了基础(我们将在另一篇文章中进行介绍)。

All code samples can be found in this GitHub repo.

所有代码示例都可以在此GitHub存储库中找到。

Thank you for reading. If this article was helpful please give it some claps ? so others can find it. I will like to read your comments also.

感谢您的阅读。 如果这篇文章有帮助,请给它一些鼓掌? 以便其他人可以找到它。 我也想阅读您的评论。

翻译自: https://www.freecodecamp.org/news/understanding-the-javascript-call-stack-861e41ae61d4/

javascript 堆栈

相关文章:

idea崩溃导致的svn插件丢失问题, maven dependencies视图丢失问题

Idea丢失Svn解决办法今天打开Idea,习惯用ctrlt来更新svn,杯具出现了,快捷键失效了,我觉得可能是其他的什么软件占用了这个快捷键,于是重启了一下,发现还是不行,svn信息怎么没了,chan…

python3代码

import urllib.request url"http://mm.taobao.com/json/request_top_list.htm?type0&page1" upurllib.request.urlopen(url)#打开目标页面&#xff0c;存入变量up contup.read()#从up中读入该HTML文件 key1<a href"http#设置关键字1key2"target&qu…

【微信小程序】侧滑栏,手动侧滑出个人中心(完整代码附效果图)

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 正文&#xff1a; 博文分三部分&#xff0c;1.效果图及功能效果说明 2.实现思路 3.源代码 欢迎加入微信小程序开发交流群&#xff08;173683895&#xff09; 一.老惯例先上效果图&#xff0c;本篇博…

1:1 人脸比对 开源_Hacktoberfest:我的开源门户

1:1 人脸比对 开源by Maribel Duran通过Maribel Duran Hacktoberfest&#xff1a;我的开源门户 (Hacktoberfest: My Gateway to Open Source) “Individually, we are one drop. Together, we are an ocean.”“就个人而言&#xff0c;我们只是一滴滴。 在一起&#xff0c;我们…

地图收敛心得170405

寻路算法大总结! 交换机生成树采用的是完全不同的D-V(distance vector)距离矢量算法,并不是很可靠. 并不是任意两点之间的最短路径,因为任意两点之间取最短路径可能有环路:总权更大 交换机STP不一定是最小生成树!!!举例论证 因为它只是所有交换机到根桥最短 贪心算法的味道 kru…

微信小程序游戏开发文档以及开发工具地址

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 正文&#xff1a; 微信官方于 2017 - 12 - 28 日 开发微信小程序 开发小游戏 &#xff0c; 微信小程序小游戏开发官方文档的地址 https://mp.weixin.qq.com/debug/wxagame/dev/index.html?t20171228…

c#编译执行过程

创建一个简单的控制台程序&#xff0c;源码如下&#xff1a; using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace csharpBuildProcess {class Program{static void Main(string[] args){for (int i 0; i < 100; i){if(i%20)…

渐进式web应用程序_渐进式Web应用程序简介

渐进式web应用程序Interested in learning JavaScript? Get my ebook at jshandbook.com有兴趣学习JavaScript吗&#xff1f; 在jshandbook.com上获取我的电子书 Progressive Web Apps (PWA) are the latest trend in mobile application development using web technologies.…

第二百二十节,jQuery EasyUI,Slider(滑动条)组件

jQuery EasyUI&#xff0c;Slider(滑动条)组件 学习要点&#xff1a; 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 Slider(滑动条)组件的使用方法&#xff0c;这个组件依赖于 Draggable(拖动)组件。 一&#xff0e;加载方式 class 加载方式 <…

适用于SharePoint 2013 的 CAML Desinger

适用于SharePoint 2013 的 CAML Desinger 分类&#xff1a; SharePoint2013-01-15 21:52 1877人阅读 评论(0) 收藏 举报CAMLDesingerSharePoint 2013代码生成适用于如果说Sql是信息管理系统的一等公民&#xff0c;那么SharePoint 系统中的一等公民就非CAML莫属了。 但是这个一等…

微信小程序 跑马灯效果完整代码附效果图

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 一&#xff1a;功能介绍及讲解 实现的跑马灯&#xff08;跑马灯里面显示文章的title&#xff09;的效果&#xff0c;并在右侧有个查看文章的按钮&#xff0c;按钮绑定当前的跑马灯信…

热闹的聚会与尴尬的聚会_如何增加(和保存)您最喜欢的技术聚会

热闹的聚会与尴尬的聚会by Jen Weber詹韦伯(Jen Weber) 如何增加(和保存)您最喜欢的技术聚会 (How to Grow (and Save) Your Favorite Tech Meetup) Hey meetup facilitators, friends, and future leaders! Do you want more people to show up to your tech event? Or at l…

蓝桥杯-搭积木-java

/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称&#xff1a; 蓝桥杯赛题 * 作 者&#xff1a; 彭俊豪 * 完成日期&#xf…

微信小程序多张图片和表单一起上传,验证表单及进度条的实现完整代码

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 效果图&#xff1a; 完整代码: <!--pages/register/register.wxml--> <view classtop> <view>注 册 须 知 : </view> </view> <view> <view …

Android, BaseAdapter 处理大数据量时的优化

Android优化 最常见的就是ListView, Gallery, GridView, ViewPager 的大数据优化 图片优化 访问网络的优化优化的原则&#xff1a; 数据延迟加载 分批加载 本地缓存数据优化 1).复用contentview 2).创建static class ViewHolder 3).分…

meetup_我在2017年举办Meetup中学到的知识以及为何对2018年充满期待。

meetupby Daniel Deutsch由Daniel Deutsch 我在2017年举办Meetup中学到的知识以及为何对2018年充满期待。 (What I’ve learned hosting Meetups in 2017 — and why I’m looking forward to 2018.) As 2017 comes to an end, it’s time to reflect on the non-profit work …

BASE64 编码和解码

依赖jar: import org.apache.commons.codec.binary.Base64; BASE64和其他相似的编码算法通常用于转换二进制数据为文本数据&#xff0c;其目的是为了简化存储或传输。更具体地说&#xff0c;BASE64算法主要用于转换二进 制数据为ASCII字符串格式。Java语言提供了一个非常好的BA…

Android开发常用属性

1、android string.xml 文字中间加入空格 android string.xml前后加空格的技巧 <string name"password">密 码</string> &#160 这个就代表着空格 2、文字单行显示 android layout布局文件中TextView、EditView单行显示和输入 <TextView androi…

JS计算起点坐标到终点坐标的驾车距离和驾车时间

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 先上计算距离的简单demo&#xff1a; <!DOCTYPE html> <html><head><meta http-equiv"Content-Type" content"text/html; charsetutf-8"&…

css flexbox模型_5分钟内学习CSS Flexbox-初学者教程

css flexbox模型快速介绍流行的布局模块 (A quick introduction to the popular layout module) In this post, you’ll learn the basics of CSS Flexbox, which has become a must-have skill for web developers and designers in the last couple of years.在本文中&#x…

「linux网络管理」OSI模型

学习linux网络管理&#xff0c;笔记整理&#xff0c;促进记忆。 OSI&#xff08;开放系统互联模型&#xff09;包含七层&#xff0c;由应用层向物理层递进&#xff0c;分别有不同的协议和数据处理方式。 应用层--> 表示层--> 会话层--> 传输层--> 网络层--> 数据…

微信小程序下拉刷新和上拉加载的实现

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 一&#xff1a; 下拉刷新 下拉刷新两个步骤就能实现。 1.在要实现下拉刷新的页面的json配置文件里面加上 "enablePullDownRefresh": true, //开启下拉刷新"backgro…

Spring整合Hibernate的步骤

为什么要整合Hibernate&#xff1f;1、使用Spring的IOC功能管理SessionFactory对象 LocalSessionFactoryBean2、使用Spring管理Session对象 HibernateTemplate3、使用Spring的功能实现声明式的事务管理 整合Hibernate的步骤&#xff1a;1、配置SessionFactory&#xff08;能够…

初创企业购买企业邮箱_支持#NetNeutrality =支持设计师及其创建的初创企业

初创企业购买企业邮箱by Lukasz Lysakowski卢卡斯吕萨科夫斯基(Lukasz Lysakowski) 支持#NetNeutrality 支持设计师及其创建的初创企业 (Supporting #NetNeutrality Supporting Designers and the Startups They Create) I believe in Net Neutrality, and I wrote a brief e…

【转】Android source build/envsetup.sh学习笔记

原文网址&#xff1a;http://blog.csdn.net/mliubing2532/article/details/7567164 如果你只需要修改某一个模块的内容&#xff0c;但是却每次都要执行make, 最后等待很长时间。使用模块编译&#xff0c;那只需要在你所在的模块的目录或者其子目录&#xff0c;执行mm&#xff0…

微信小程序之上传附件

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 目前微信API开放上传图片&#xff0c;录音文件&#xff0c;视频文件&#xff0c;but 只能下载并打开附件&#xff0c;不能上传附件&#xff0c;以后会不会开放附件上传目前还不确定&…

微软在.NET官网上线.NET 架构指南频道

微软在Visual Studio 2017 正式发布的时候也上线了一个参考应用https://github.com/dotnet/eShopOnContainers , 最近微软给这个参考应用写了完善的文档&#xff0c;放在.NET官网的.NET架构频道https://www.microsoft.com/net/architecture。 整个.NET 架构按照4个部分展开&…

初学者css常见问题_5分钟内学习CSS Grid-初学者教程

初学者css常见问题Grid layouts are fundamental to the design of websites, and the CSS Grid module is the most powerful and easiest tool for creating it. I personally think it’s a lot better than for example Bootstrap (read why here).网格布局是网站设计的基础…

HBase保存的各个字段意义解释

// Author&#xff1a;xxx0624 HomePage&#xff1a;http://www.cnblogs.com/xxx0624/ // nutch2.2.1集成HBase0.94.25, 可以查询nutch的conf文件中的gora-hbase-mapping.xml查看原文件 <gora-orm><table name"webpage"><family name"p" ma…

AJAX基础篇

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 正文&#xff1a; 整理一下AJAX相关的知识&#xff0c;帮助自己复习的同时希望还能够帮助到新加入前端阵营的小伙伴们。 1.AJAX是什么&#xff1f; AJAX 异步的JavaScript和XML 2.AJAX的作用&…