3

If you have dreams and dry goods, search [Big Move to the World] attention to this Shawanzhi who is still washing dishes in the early morning.

This article has been included in GitHub https://github.com/qq449245884/xiaozhi . There are complete test sites, materials and my series of articles for interviews with first-tier manufacturers.

Question 1: What is the output of the following code

let object_1 = { name: '小智' }
let object_2 = object_1
object_1.name = '王大志'
console.log(object_2)

output

{ name: '王大志' }

Reason: We can see that the assignment operator directly passes the address (reference) when passing non-primitive data types. Therefore, non-primitive data types are always passed by reference.

Question 2: What is the output of the following code

var a = 10
var show = function () {
  console.log(a)
  a = 20
}
show()

output 10

Reason: scope hoisting

Question 3: What is the output of the following code

console.log(name)
hello()
function hello() {
  console.log('你的名字')
}

var name = '小智'

output:

undefined
你的名字

I'm confused, why is undefined , I don't understand, do you know the answer?

Question 4: What is the output of the following code

let x = true
setTimeout(() => {
  x = false
})
while (x) {
  console.log('hello')
}

output

无限打印 'hello'

what is the reason? We know that SJ is a single-threaded language, and setTimeout is an asynchronous operation, so it will enter the task queue and wait for a chance to execute.

Because x is true , it will enter "while loop", it will print "hello" continuously, after 2 seconds, setTimeout ready to execute, but the call stack is not empty, it is already running the while loop, so setTimeout will not get any execution opportunity.

Question 5: What is the output of the following code

let x = true
let count = 0
setTimeout(() => {
  x = false
}, 2000)
setInterval(() => {
  if (x) {
    console.log(count++)
  }
}, 200)

output:

0
1
2
3
4
5
6
7
8

Reason: setTimeout and setInterval are an asynchronous operation, so first, it calls setInterva .
, while setInterval will print the value of 062439d8b9bea2 every count , then setTimeout after 2 seconds
will be called, at which point x is changed to false . setInterval condition is not satisfied, so the value of count will not be printed, and then continue to execute.

The bugs that may exist after the code is deployed cannot be known in real time. In order to solve these bugs afterwards, a lot of time is spent on log debugging. By the way, I recommend a useful bug monitoring tool Fundebug .

communicate with

If you have dreams and dry goods, search [Moving the World] attention to this brush bowl wisdom who is still washing dishes in the early morning.

This article has been included in GitHub https://github.com/qq449245884/xiaozhi . There are complete test sites, materials and my series of articles for interviews with first-tier manufacturers.


王大冶
68k 声望104.9k 粉丝