当前位置:网站首页>Using an example to understand the underlying processing mechanism of JS function
Using an example to understand the underlying processing mechanism of JS function
2020-11-06 20:53:00 【mttwind】
Personal notes , Please correct me if there is any mistake
Take the following code as an example , Run the parsing line by line
var x = [12, 23];
function fn(y) {
y[0] = 100;
y = [100];
y[1] = 200;
console.log(y);
}
fn(x);
console.log(x);
var x = [12, 23];
Run the following
- Open up heap memory , Create array values , Suppose the address of the heap memory is 0x000000
- Declare variables
x
- assignment , the
x
Address to heap memory 0x000000
next
function fn(y) {
y[0] = 100;
y = [100];
y[1] = 200;
console.log(y);
}
Run the following
The above code is the process of creating a function . It's similar to creating a variable :
- They all declare a variable to store the value
- Same steps : The first step is to create a heap memory , There are functions in it , This heap has an address , Then assign the address to the variable
- Statement : In a similar way, function names are variables , When we declare functions
function fn(y){...}
when , It's equivalent to declaring a variable , It's just that the value is a function . Be similar tovar fn = function (y){...}
Function expression for . Finally, assign a function as a value to a variable or something else
So create a function , The detailed execution order is as follows
- Start with a heap of memory , Store the value of the function ( Suppose the address is 0x000001)
- The value of the object is in heap memory , It stores its key value pairs
- The value of the function is in heap memory , It's stored in its code , And it's stored in the form of strings
- When you create a function , It's a statement of its Scope (scope),scope The value is The context in which the function is currently created , That is, in which context , The scope is who
- Then declare the variable
fn
, And point to the heap memory address ( Assuming that 0x000001)
To perform a function
fn(x);
Run the following ( To perform a function )
- Function execution time , The value is always passed on ,
fn(x)
The message isx
Value , namelyx
Point to the 0x000000 Heap memory address fn(0x000000)
Form a brand new Private context EC(fn)- In the new context of function formation , Generate a private variable object AO, Used to store variables declared in the current context (Active Object The object of activity , abbreviation AO, A variable object , Similar to global variables in the global context )
- What happened before the internal code was executed
- Initialize scope chain scope-chain <EC(fn1),EC(G)>, At both ends of the chain are < Your current private context , The scope of the function ( The context in which the function was created )>, The right side of the chain is also called the current context ' Superior context '
- initialization this
- initialization argument
- In the current context , Declare a parameter variable , And assign the value of the argument passed to it
- Variable Promotion
- Stack execution code
- Release from the stack
Function stack execution code detailed steps
Let's move on to the top 5 Step by step
Put the previously created function , The code string stored in heap memory is taken out and converted into line by line execution of code .
If a variable is encountered during code execution in private context , First of all, see if it's for your own ' Private variables ', If it is ' private ' Of , Then operate your own , There is no necessary relationship with the outside world , If it's not private , Based on Scope chain , To whom Superior context Search for , See if it is private in the context of the superior , If it's not , Continue looking up ...... Always find EC(G) Global context until , We call this search process Scope chain lookup mechanism therefore
y[0] = 100
y = [100]
y[1] = 200
It's done like this :
-
y[0] = 100
,y
Now the memory address stored is 0x000000, So change the value under this address : -
y = [100]
, New object values appear , So we need to open up new heap memory 0x000002, Create values , assignment -
y[1] = 200
, take 0x000002 Modify the value of the object corresponding to the address -
console.log(y);
Thisy
Namely 0x000002 Corresponding value[100,200]
It operates on private variablesy
fn
This completes the function .
And then execute the outside console.log(x);
, At this time x In the global variable object x, The corresponding address is 0x000000, So output directly [100,23]
If fn
After execution , Continue with other functions , It's going through the same process . Form a new context , Stack execution ... If there are so many functions , It's going to keep going up the stack , It's going to take up more and more memory . So in order to optimize , Browsers will default to many recycling mechanisms
Results and overall process
result
General flow chart
Other explanatory points
js Context classification
js Context ( Which area is executed ) classification
- Global context EC(G)
- Private context formed by function execution
- Block level private context
What are private variables
Private variables are variables declared by private context , contain
- Shape parameter
- Variables declared at the time of code execution
var
/let
/const
/function
... Note the difference from global variables , Not directly related , But there may be some indirect relationship , For example, the following code under the global variablex
The value of is 0x000000, By function , take 0x000000 Passed to private variablesy
,y
It's also 0x000000
function fn(y) {
y[0] = 100;
y = [100];
y[1] = 200;
console.log(y);
}
fn(x)
版权声明
本文为[mttwind]所创,转载请带上原文链接,感谢
边栏推荐
- 一路踩坑,被迫聊聊 C# 代码调试技巧和远程调试
- Live broadcast preview | micro service architecture Learning Series live broadcast phase 3
- Filecoin has completed a major upgrade and achieved four major project progress!
- Ronglian completed US $125 million f round financing
- 游戏主题音乐对游戏的作用
- html+vue.js 實現分頁可相容IE
- 【字节跳动 秋招岗位开放啦】Ohayoo!放学别走,我想约你做游戏!!!
- Kubernetes and OAM to build a unified, standardized application management platform knowledge! (Internet disk link attached)
- CloudQuery V1.2.0 版本发布
- jenkins安装部署过程简记
猜你喜欢
Filecoin has completed a major upgrade and achieved four major project progress!
An article will take you to understand CSS3 fillet knowledge
How to turn data into assets? Attracting data scientists
Outsourcing is really difficult. As an outsourcer, I can't help sighing.
Pn8162 20W PD fast charging chip, PD fast charging charger scheme
jenkins安装部署过程简记
每个大火的“线上狼人杀”平台,都离不开这个新功能
The AI method put forward by China has more and more influence. Tianda et al. Mined the development law of AI from a large number of literatures
PHP application docking justswap special development kit【 JustSwap.PHP ]
What is the meaning of sector sealing of filecoin mining machine since the main network of filecoin was put online
随机推荐
Application of restful API based on MVC
What are the criteria for selecting a cluster server?
Read the advantages of Wi Fi 6 over Wi Fi 5 in 3 minutes
检测证书过期脚本
[Xinge education] poor learning host computer series -- building step 7 Simulation Environment
行为型模式之备忘录模式
意外的元素..所需元素..
Kubernetes and OAM to build a unified, standardized application management platform knowledge! (Internet disk link attached)
一部完整的游戏,需要制作哪些音乐?
Summary of front-end performance optimization that every front-end engineer should understand:
nacos、ribbon和feign的簡明教程
MongoDB与SQL常用语法对应表
Behind the record breaking Q2 revenue of Alibaba cloud, the cloud opening mode is reshaping
JNI-Thread中start方法的呼叫與run方法的回撥分析
Live broadcast preview | micro service architecture Learning Series live broadcast phase 3
It's time for your financial report to change to a more advanced style -- financial analysis cockpit
大道至简 html + js 实现最朴实的小游戏俄罗斯方块
Helping financial technology innovation and development, atfx is at the forefront of the industry
如何在终端启动Coda 2中隐藏的首选项?
Basic principle and application of iptables