Frontend Developer 面试题与答案
问题 11. Explain the concept of 'hoisting' in JavaScript.
'Hoisting' is a JavaScript behavior where variable and function declarations are moved to the top of their containing scope during the compilation phase. However, only the declarations are hoisted, not the initializations.
Example:
```javascript
console.log(x); // Outputs: undefined
var x = 5;
```
问题 12. What is the difference between `let`, `const`, and `var` in JavaScript?
The main difference lies in their scoping and hoisting behavior. `let` has block scope, `const` is used for constants, and `var` has function scope and is hoisted.
Example:
```javascript
// Example using let
let x = 10;
// Example using const
const PI = 3.14;
// Example using var
var y = 5;
```
问题 13. Explain the concept of closures in JavaScript.
Closures allow a function to access variables from its outer (enclosing) scope even after the outer function has finished executing. They help in creating private variables and methods.
Example:
```javascript
function outer() {
let x = 10;
function inner() {
console.log(x);
}
return inner;
}
const closureExample = outer();
closureExample(); // Outputs 10
```
问题 14. What is the purpose of the 'use strict' directive in JavaScript?
The 'use strict' directive enforces a stricter set of parsing and error handling rules in JavaScript. It helps catch common coding errors and prevents the use of certain error-prone features.
Example:
```javascript
'use strict';
// Strict mode code goes here
```
问题 15. Explain the box model in CSS.
The box model consists of content, padding, border, and margin. It defines how these components contribute to the overall size of an element.
Example:
```css
.box {
width: 200px;
padding: 20px;
border: 2px solid #000;
margin: 10px;
}```
用户评价最有帮助的内容: