2020 interview questions

1, is determined js type manner

Solution: A , typeof (based on the 'String', 'Number', 'Boolean', 'undefined', 'Symbol' , but the determination typeof (null) value is 'object'; Analyzing time when both arrays and objects 'Object' ); B , the instanceof ( var STR = new new String ( 'Hello' ); the console.log (STR the instanceof String); // to true ).

2 , ES5 and ES6 are several ways to declare variables

Solution: A , the ES5 there are two kinds: var  and  function ; B , for ES6 are six: four kinds increases, the let , const , class  and  import

3 , closure concept? Advantages and disadvantages of closing package?

Solution: Concept: closure is the internal variable can be read as a function of other functions.

advantage:

A , to avoid contamination of global variables

b , want a long-term variable stored in memory (cache variable)

Disadvantages:

A , memory leaks (consumption)

b , permanent memory, increased memory usage

4 , deep and shallow copy copy

Solution: Meaning: Suppose B copy of A, when modifying A, B to see whether the change would happen if B also followed changed, stated that this is a shallow copy; take short of manpower, if B has not changed, and that is a deep copy.

Shallow copy:

let a=[0,1,2,3,4],

     b=a;

console.log(a===b);   //true

a[0]=1;

console.log(a,b);  1,1,2,3,4

Deep copy:

let a=[0,1,2,3,4],

     b=a.slice();

a[0]=2;

console.log(a,b);  // a:2,2,3,4  b:1,2,3,4

 

function deepClone(obj){

let objClone = Array.isArray(obj)?[]:{};

    if(obj && typeof obj==="object"){

        for(key in obj){

            if(obj.hasOwnProperty(key)){

                // determine whether the object is ojb child element, if it is, recursive copy

                if(obj[key]&&typeof obj[key] ==="object"){

                    objClone[key] = deepClone(obj[key]);

                }else{

                    // If not, simply copy

                    objClone[key] = obj[key];

                }

            }

        }

    }

    return objClone;

}    

let a=[1,2,3,4],

b=deepClone(a);

a[0]=2;

console.log(a,b);// [2,2,3,4]  [1,2,3,4]

5, the array method of deduplication

solution:

f1: .ES6 the Set:

let arr = [1,1,2,3,4,5,5,6]

let arr2 = [...new Set(arr)] // [1,2,3,4,5,6]

f2:reduce:

let arr = [1,1,2,3,4,5,5,6]

let arr2 = arr.reduce(function(ar,cur) {

   if(!ar.includes(cur)) {

     Arkpus (Cur)

   }

   return ar

},[])

f3:filter:

// This method will have a problem: [1, '1'] is the same element as the final input [1]

let arr = [1,1,2,3,4,5,5,6]

let arr2 = arr.filter(function(item,index) {

  // indexOf () method returns a string value of the specified position of the first occurrence of a string

  return arr.indexOf(item) === index

})

6, enter what URL happen?

solution:

a, DNS DNS (domain names into ip address, go UTP protocol, so there is no handshake): browser URL parsing a corresponding server IP address (1. DNS cache on the local browser to find 2. again the system DNS cache send request 3. DNS cache again router 4. the network operator DNS cache recursive search), and from the parse out the port number of the url;

b, the browser and the target server to establish a TCP connection (three-way handshake);

c, the browser sends to the server an HTTP request;

d, the server returns to the browser a HTTP response message;

e, the browser for rendering;

f, close the TCP connection (four wave).

Solution 2:

1. Enter the URL;

2. sent to the DNS server, and acquires the domain name corresponding to the ip address corresponding to a web server;

3. to establish a TCP connection to a web server;

4. browser to send an http request web server;

5.web server responds to the request and returns the data (or an error message, or redirect url new address) to specify the url;

6. The browser downloads the data returned by the web server and parse html source file;

7. generated DOM tree, and the parsing css js, rendering the page, until the display is completed;

 

7, GET and POST requests difference

solution:

GET url passed parameters, POST placed in the body;

GET request parameters passed in the url is the length restricted, but no POST;

GET fallback when the browser is harmless, and POST will submit the request again;

GET requests are active cache browser, POST will not, unless manually set;

POST is more secure than GET, because the url parameter directly exposed, it can not be used to transmit sensitive information to the data type of the parameter, GET accepts only ASCII characters, while POST is not limited;

GET requests only url (x-www-form-urlencoded) encoding, and support a variety of encoding POST;

GET generates a TCP packet; generating the POST two TCP packets. For the GET request, the browser will http header and the data sent along with the server response 200 (return data). For POST, the browser transmits the first header, the server response 100 continue, the browser then transmits data, in response to the server 200 ok (return data)

8, HTTPS than HTTP

solution:

HTTPS protocol requires the CA to request a certificate, the certificate is generally free little need to pay.

HTTP protocol runs on top of TCP, all the contents are transmitted in clear text, HTTPS runs over SSL / TLS, SSL / TLS runs over TCP, the contents of all transmissions are encrypted.

Using HTTP and HTTPS connection is completely different, with the port are not the same, the former is 80, which is 443.

HTTPS can effectively prevent hijacking operators to solve a big problem of anti-hijack.

9, the difference watch, methods and computed in?

solution:

a, the data to monitor changes in response to a watch. automatically monitor change in dependence computed values ​​to dynamically return, a main object is to simplify the complex calculation in the template. So the difference comes from the usage, just need dynamic values, then use computed; execute business logic needs to know the value of the change, only with a watch.

b, methods is a method that can accept parameters, and can not be computed, the cache can be computed, not Methods. can be computed independent of other computed, or even other data components.

10, an array of related

solution:

  1. The length of the push () additive element from the rear, the return value is added after the array
  2. pop () to remove elements from the back, is only one element of the return value is deleted
  3. shift () returns a value in front of deleting elements, it can only be deleted from the deleted elements
  4. the unshift () to add elements from the foregoing, the return value is added after the length of the array
  5. concat () returns an array of two new array is connected to the connection
  6. split (i, j) is converted to a string array i-- separator, default for all null characters, including spaces, linefeed (\ n-), tab (\ t) and the like. j-- split times. Defaults to -1, i.e., all the partition
  7. sort () to sort the array, good return value is an array of rows, the default is sorted according to the leftmost digit is not sorted according to the size of the figures
  8. reverse () array reversed, the return value is an array of the inverted
  9. Slice (start, end) to cut the index start end of the array index value, the index does not contain the end value, the return value is an array of cut out  
  10. forEach (callback) to iterate, no return callback parameters: value - the current value of the index index - the index array - the original array
  11. Map (callback) mapping array (iterate), there is an array of return to return a new callback parameters: item-- current value of the index index - the index arr - original array
  12. filter (callback) filter array, a return to meet the requirements of the array
  13. join () method for all elements in the array into a string. Elements are separated by a specified delimiter. The default is,;

13, Promise.all () usage

Solution: Example Promise.all () batch execution, Promise.all ([p1, p2, p3]) a plurality of promise for instance, packaged into a new instance Promise, return is common promise

Guess you like

Origin www.cnblogs.com/zaijin-yang/p/12509930.html