How JavaScript interpreter interpret code?

别说谁变了你拦得住时间么 提交于 2021-01-29 02:08:18

问题


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();

Questions

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) 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) 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?

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) 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 ().

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) 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.

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.




回答2:


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

Crates two variables in the scope of the window, basically global variables. Being fName and lName.

function myName (fName, lName) {

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

  return fName + ' ' + lName;

}

The variables here are parameters (fName, lName), are treated as local variables, but parameters don't need to be defined by var, they are different from the global variables fName and lname.

fName; // "John"
lName; // "Snow"
myName(); // "Sam Doe"



回答3:


First of all, JavaScript is loosely types language, which means whatever the statement is expecting a data-type, JavaScript will automatically convert the data to that type. JavaScript allocates the memory when values are initialised to the respective strings, arrays, objects etc...

var a = 123;   //Allocates memory for number
var b = "program"   //Allocates memory for string

When you create a function a function with arguments those arguments treated as a local variables, you can initialise the values at the time of creating arguments also.

Before the execution of that snippet from top to bottom. First you should understand how interpreter works. "An interpreter checks the errors of a program statement by statement. After checking one statement, it converts that statement into machine code and then executes that statement. The process continues until the last statement of program occurs." 1. First memory allocated for tow global variables 2. Function is skipped 3. at last function gets called and returns the value Please check this Loupe visualisation to understand how JavaScript's call stack/event loop/callback queue interact with each other



来源:https://stackoverflow.com/questions/31638529/how-javascript-interpreter-interpret-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!