Javascript - 上下文执行阶段 [英] Javascript - Context Execution phases

查看:94
本文介绍了Javascript - 上下文执行阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道JavaScript中的执行分两个阶段发生:

I know that the "execution" in JavaScript happens in 2 phases:

1)将函数和变量添加到内存中时的创建阶段,提升,这是创造和外部环境

1) The Creation phase when the functions and variables are added in the memory, hoisting,the this is create and outer environment

2)代码执行的第二阶段。

2) The second phases when the code is executed.

问题:变量和函数内部是在脚本启动时或者只在调用父函数时在内存中添加父函数?

The question: The variables and functions insides a parent function are added in the memory when the script start or only when the parent function is invoked ?

参数的行为之间有任何区别函数和函数内部声明的变量,关于这个方面? (我问这个是因为在其他语言中它们表现不同 - 参数在函数执行前在内存中,但在函数范围内)

There is any difference between behavior of parameters of a function and variables declared inside function, regarding this aspect ? (I ask this because in other languages they behave different - the parameters are in memory before function executes, but in the scope of function)

推荐答案

如果脚本启动时所有函数内的变量和函数都已放入内存,则内存可能在启动时已满。当函数执行时,变量和内部函数被添加到内存中。当函数结束执行时,相关变量将从内存中删除,如果它们未在 关闭

If the variables and functions inside all functions were been put into the memory when the script starts, you memory may be full at start. When a function executes, at that time the variables and inside functions are added into the memory. When the function ends its execution, the related variables are removed from the memory, if they are not used in closure.


参数的行为有任何区别函数
和函数内部声明的变量

There is any difference between behavior of parameters of a function and variables declared inside function

行为没有明显的差别,参数也是变量在函数范围内声明,只有它们可以从外部获得它们的值。

There is no obvious deference in the behavior, the parameters are also variables which are declared in the scope of the function, only they can have their values from the outer.

当您将引用类型传递给函数并更改其属性时,可能存在一个区别在功能上。因此它将在函数本身之外进行更改。这意味着参数可以某种方式限制在外部世界。但我认为这与参数和范围变量的行为无关。它们都是作用于函数。

One difference can be when you passed a reference type to the function and change its property in the functions. So it will be changed outside the function itself. This means that parameters can be bounded to the outer world in some manners. But I think this is not related to the behavior of parameters and scoped variables. They both are scoped into the function.


参数在函数执行前在内存中,但在
函数范围内

the parameters are in memory before function executes, but in the scope of function

Javascript函数可以使用不同数量的参数。所以它无法将参数添加到内存中。你可以有一个没有参数的函数,但是调用它并传递10个参数。

Javascript functions can take varied number of parameters. So it can't add the parameters to the memory. You can have a function with no parameters, but call it and pass 10 parameters to it.

这篇关于Javascript - 上下文执行阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆