JavaScript解释器如何解释代码? [英] How JavaScript interpreter interpret code?

查看:77
本文介绍了JavaScript解释器如何解释代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解浏览器如何解释javaScript.我有以下代码

I am trying to understand how javaScript is interpreted by the browser. I have following code

var fName = "John";
var lName = "Snow";

function myName (fName, lName) {

  fName = "Sam";
  lName = "Doe";

  return fName + ' ' + lName;

}

fName;
lName;
myName();

问题

1)解释代码后,引擎是否首先为变量从上到下先分配内存,然后分配值,或者同时逐行完成两个操作?

1) When the code is interpreted does the engine first allocate memory for the variables from top to bottom first and then assign the values then or in the same time both operations are done by line by line?

2)在函数 myName 中,当声明了 fName lName 时,是否将它们创建为局部范围变量,如果可以,为什么不创建允许在()中使用var关键字.

2) In function myName when fName and lName are declared are they are created as local scope variables, if yes why it is not allowed to use var keyword in the ().

3)如果有人可以解释该代码从上到下的解释方式,我将不胜感激.

3) I would really appreciate it if someone could explain how this code is interpreted top to bottom.

推荐答案

1)解释代码后,引擎首先分配内存首先从上到下的变量,然后分配值那么还是在同一时间,这两项操作都是逐行完成的?

1) When the code is interpreted does the engine first allocates memory for the variables from top to bottom first and then assign the values then or in the same time both operations are done by line by line?

解析代码时,将创建定义的全局标识符( fName lName myName ).该函数已创建并分配给其标识符,但尚未为变量分配任何值.

When code is parsed, the defined global identifiers (fName, lName and myName) are created. The function is creted and assigned to its identifier, but the variables are not assigned any value yet.

变量的创建和分配是分开的.代码中定义的所有变量在代码启动之前就存在.您可以为在代码中进一步定义的变量分配一个值(但这很容易造成混淆).示例:

The creation of the variables and the assignment is separate. All variables defined in the code exist before the code starts. You can assign a value to a variable that is defined further down in the code (but that easily gets confusing). Example:

fName = "John";
lName = "Snow";
var fName, lName;

2)在函数myName中,当声明了fName和lName时,它们是创建为局部作用域变量,如果是,为什么不允许使用它()中的var关键字.

2) In function myName when fName and lName are declared does they are created as local scope variables, if yes why it is not allowed to use var keyword in the ().

在函数中定义的参数在函数中创建为局部变量.就像参数有一个隐式的 var 一样,因此它不是必需的(也是不允许的).

The parameters defined in the function are created as local variables in the function. It's as if there is an implicit var for the parameters, so it's not needed (and not allowed).

通过声明局部变量并将参数值分配给它们,您可以获得基本相同的效果:

You can get basically the same effect by declaring local variables and assign the argument values to them:

function myName() {
  var fName = arguments[0];
  var lName = arguments[1];
  ...

3)如果有人可以解释此代码,我将不胜感激从上到下解释.

3) I would really appreciate it if someone could explain how this code is interpreted top to bottom.

执行代码时,代码从上到下运行.为全局变量分配它们的值,将其作为定义跳过,然后对全局变量求值并忽略结果,然后调用该函数.

When code is executed, the code runs from top to bottom. The global variables are assigned their values, the function is skipped as it is a definition, then the global variables are evaluated and the result is ignored, then the function is called.

在该函数中,将创建参数并为其分配发送给它的值.由于您不发送任何值,它们将具有值 undefined .在代码中,为参数分配了新值,然后串联并返回了这些新值,但是调用代码将忽略函数的返回值.

In the function the parameters are created and assigned the values that you send into it. As you don't send any values, they will have the value undefined. In the code the parameters are assigned new values, then those new values are concatenated and returned, but the return value from the function is ignored by the calling code.

这篇关于JavaScript解释器如何解释代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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