Home  >  Article  >  Web Front-end  >  Common interview questions about JavaScript

Common interview questions about JavaScript

微波
微波Original
2017-06-28 13:26:243521browse

Common interview questions about JavaScript

Regarding the JS scope issue, it is an interview question that interviewers never tire of asking during interviews. Sometimes it is really frustrating. The following article mainly shares with you javascriptSome common interview questions about scope, friends in need can refer to them. Let’s take a look below.

Recommended related articles:The most complete collection of js interview questions in 2020 (latest)

This article mainly shares with you the relevant content about the JavaScript scope interview questions, and shares it for your reference and study. Let’s take a look together.

1. Scope:

Before understanding scope, you first need to understand some basic concepts:

Every variable, function has its scope, and it cannot be used beyond the scope. This is called scope.

2. Global variables, local variables:

1. Global variables:

 (1) Variables declared in the global scope, such as var a=1;

 (2) Only assigned values ​​without declaration, such as a= 2; (Note: If a=2 is in the function environment, it is also a global variable)

2. Local variables:

Variables written into functions are called local variables.

3. Function:

(1) Program safety.

(2) Release of memory.

3. Scope chain:

The process of finding the quantity. First, check whether there is a declaration or function in your local environment. If there is, check whether the declaration has an assignment or the content of the function. If not, search up one level.

4. Pre-parsing sequence:

Every program has to do the work. The program starts by pre-parsing the grammar and whether there are punctuation marks. If there is an error, analyze whether the memory can be accommodated, analyze the variables... until the analysis is correct, start to follow the normal process sequence. Just imagine, if there is no pre-parsing sequence and the process sequence is followed directly, the program may execute until the last function, find syntax error, and then start to report errors. How bad will the performance be!

Sequential content:

## 1. The 3f1c4e4b6b16bbbd69b2ee476dc4f83a blocks referenced in the file are parsed in sequence and connected from top to bottom.


2. The var inside each script block (note: only

variable name is parsed, and the value is not parsed, such as var a=2;replace var a is parsed at the beginning of the environment, and subsequent values ​​are not parsed. Only when the program is executed to the line var a=2, the variable will be assigned a value), and function is parsed to the beginning of this block.

3. Parse each environment in turn, parsing

var, function to the beginning of the environment.

5. Application scenarios (some common scope-related interview questions):

var a="aa";
function test(){
 alert(a);//undefined,函数执行后,在函数环境内,var a会预解析,当弹出a时,首先先找本层环境内有无声明,发现有。但是代码没有执行到赋值,所以结果是undefined。
 var a="bb";//var a会预解析在函数开头,执行到这行才进行赋值
 alert(a);//“bb”
}
test();
alert(a);//"aa" 找全局环境下的声明,找到了var a="aa"
var a="aa";
function test(){
 alert(a);//“aa”,函数执行后,在函数环境内,没有找到本层环境关于a的声明,所以开始向上一层环境查找。
 a="bb";//执行到这行开始改变全局a的量
}
test();
alert(a);//"bb" 全局环境的a在函数执行时已经被改变
function test(){ 
 b();//函数b会被预解析,因此可以调用,执行了输出1;
 var a=1;
 function b(){
  console.log(1);
  console.log(a);//undefined
  var a=2;
 }
}
test();

6. Summary :

To figure out the scope of a

variable, the key is to figure out the pre-parsing order, and then judge the scope of the scope. These are There is a routine: first check whether there is a declaration in the environment of this layer, and if so, check whether assignment has been performed; if there is only a declaration but no assignment, it is undefined. If there is no declaration or assignment, search one level higher until it is found. If all execution environments are not found, the console will report an error that the variable cannot be found.

For functions, it’s even simpler: find out if there is a pre-parsed function in this layer’s environment, and if so, you can execute it. If not, still look up.

Related learning recommendations:

javascript video tutorial

The above is the detailed content of Common interview questions about JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn