Interview summary

I'm almost busy with the thesis, so summarize the questions during the interview during this time.

1. What is the following output?

1 var a = [1,2],  b = [3],  c= 6;
2 function test(a1, b1, c1){
3        a1= [];
4        b1[0] = 4;
5        c1= 7;
6 }
7 test(a,b,c);
8 console.log(a, b, c);

This question can be done by distinguishing the two data types. The array is a reference data type. The essence is that an object is referenced through an address value. Test() does not change the attribute (index value) of the array a, nor does it change a The referenced object address value, so a remains unchanged, and the output is still [1,2]

The index value of b has changed, which is equivalent to changing its attribute value, so the output b becomes 4

c is a value of type Number. The operation in test() actually copies the value of c and takes it for the operation, which does not affect its own value, so it is still 6

The end result is:

[1,2] 4 6

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324768728&siteId=291194637