1, what are the data types of JS, and what are the basic data types

Boolean, NULL, Undefined, Number, String, Object, Symbol (ES6 new definition) the first five basic types, ES6 out of the Symbol is the original data type, indicating that the unique value Object is a reference type, the range is quite large. The ES2020 (ES11) function also adds a new type, BigInt, which is intended to be a wider range of integer values than the Number data type supportsCopy the code

2. Determine the data type

(1) Typeof judgment all the value type (undefined, string, number, Boolean, symbol) to determine whether the function / / function to judge whether the object reference type (null, [1, 2, 3]. {a: 2}) (2) Instanceof is used to determine whether A is an instanceof B. For example, an Instanceof B instance can only be used to determine whether two objects belong to the instance relationship, but not to determine what type an object instance belongs to. Therefore, it is generally used to determine whether an object instance is an array. [] instanceof Array (3) the Object. The prototype. ToString. Call the toString () () is the Object of the prototype method, this method is invoked, By default, returns the current object's [Class], which is an internal attribute of the form [object Xxx], where Xxx is the type of the object. For object objects, toString() returns [Object Object]. For other objects, Constructor is a property of prototype. When a function is defined, The js engine adds a prototype to the function, and the constructor property in that prototype points to the function reference, so overwriting prototype loses the original constructor. 1: null and undefined undefined constructor In addition, if the developer overwrites prototype, the original constructor will be lost. Therefore, in order to standardize development, it is generally necessary to re-assign constructor when rewriting object prototypes to ensure that the type of the object instance is not tampered with.Copy the code

What is a closure

To understand closures, we need to understand the JS garbage collection mechanism, which means that when a function is executed, its scope is reclaimed. If a closure is formed, its scope is not reclaimed after execution. A closure is formed when a function is referenced by a variable other than its parent function. The purpose of a closure is to store a variable that is private to the outside world through an interface (method) that is not directly accessible to the outside world.Copy the code

4. Same-origin policy and cross-domain communication

The same origin policy restricts how documents or scripts loaded from the same source can interact with resources from another source. This is an important security mechanism for isolating potentially malicious files. If a protocol, domain name, or port is different, they are different sources. Cross-domain communication (1) JSONP: The introduction of JS script files in different domains on the page is not restricted by the same origin policy. So after the JS file is loaded, the callback is triggered and the required data can be passed in as an argument. Advantages: Good compatibility (compatible with earlier versions of IE) Disadvantages: JSONP only supports GET requests (2) CORS: The Origin value of the Request header is compared with the value of the access-Control-request-method and access-Control-request-method Headers. If the Request succeeds, the Request fails. PostMessage (H5), window.postMessage(message,targetOrigin) is a new feature of HTML5 that can be used to send messages to other window objects. Whether the window object belongs to the same or a different source. (4) Node reverse proxy: If we use the front-end service from Node, we can use Node to directly reverse proxy, import a plug-in to handle the proxy, and then configure a 'target'. (5) Nginx reverse proxy: Hash (window.location.hash + iframe) Hash (6) WebSocket (7) Hash (window.location.hash + iframe)Copy the code

5. This refers to the problem

(1) The ordinary function call this points to Windows; (2) The object function calls this to refer to the object; (3) The constructor calls this to the current instance itself (the newly created object); (4) Call and apply, bind what is passed to what, the parameters passed; (call can pass multiple arguments, apply can pass two arguments, the second array, otherwise TypeError) (5) Arrow function calls this to the value of the parent scope (current function context);Copy the code

6. Prototype and prototype chain

Creating a function creates a Prototype property for it that points to the function's prototype object, which automatically gets the constructor property pointing to the function whose Prototype property belongs. When an object calls a method or property, it looks for it, if it finds it, it calls it, if it doesn't find it, it follows __proto__ to the prototype object, if it doesn't find it, it continues to look for it in the prototype until null, forming a chain called the prototype chain. Return undefined if not found.Copy the code

Find the specified element in the array

The (1) includes() method is used to determine if an array contains a specified value and returns true if it does, false otherwise. IndexOf () returns the first indexOf the specified element in the array, or -1 if none exists. (3) The lastIndexOf() method returns the index of the last element in the array, or -1 if none exists. Look forward from the back of the array, starting at fromIndex. (4) The some() method tests that at least one element in the array passes the provided function test. It returns a Boolean value. (5) The every() method tests whether all elements in an array pass the test of a given function. It returns a Boolean value. (6) The filter() method creates a new array containing all the elements of the test implemented by the provided function. (7) The find() method returns the value of the first element in the array that satisfies the provided test function. Otherwise return undefined. (8) The findIndex() method returns the index of the first element in the array that satisfies the provided test function. Returns -1 if no corresponding element is found.Copy the code

8. Run result (test js pre-parsed variable promotion, this pointing problem)

<script> a = 1 fun(a) var a = 0 function fun(num) {console.log(num + this.a) Function fun(num) {console.log(num + this.a)} a= 1 fun(a) a=0 </script> This refers to Windows, so when the function is run, num is 1, this.a is also 1, and the result is printed as 2Copy the code

The difference between GET and POST

(1) Different functions

I. Get gets data from the server. ⅱ. Post is to send data to the server.Copy the code

(2) Different processes

ⅰ. Get is to add the parameter data queue to the URL indicated by the ACTION attribute of the submitted form. The value corresponds to each field in the form, which can be seen in the URL. ⅱ. Post is the HTTP POST mechanism. Each field in the form and its contents are placed in the HTML HEADER and sent together to the URL address indicated by the ACTION attribute. The user does not see this process.Copy the code

(3) Different acquisition values

For get, the server uses Request.QueryString to obtain the value of a variable. ⅱ. In POST mode, the server uses Request.Form to obtain submitted data.Copy the code

(4) Different amounts of data are transmitted

I. The amount of data transmitted by GET is small and cannot be larger than 2KB. ⅱ. The amount of data sent by POST is large, and is generally regarded as unrestricted by default.Copy the code

(5) Different security

I, get security is very low. ⅱ. Post has high security. - Get is passed through the URL and POST is placed in the request body. - The parameters passed by the GET request in the URL are length limited, but the parameters passed by post are not. - GET is less secure than POST because parameters are directly exposed in the URL and therefore cannot be used to pass sensitive information. - GET requests can only be url encoded, but POST supports multiple encoding modes. - GET requests can be active cache by the browser, while POST supports multiple encoding modes. - GET request parameters are retained in browsing history, but parameters in POST are not. - GET and POST are essentially TCP connections. However, due to HTTP regulations and browser/server restrictions, they are applied differently. - GET generates a TCP packet. POST generates two TCP packets.Copy the code

Js timer, difference and usage

(1) The setTimeout() method is used to call a function or evaluate an expression after a specified number of milliseconds. (2) The setInterval() method calls a function or evaluates an expression at a specified interval in milliseconds. The setInterval() method keeps calling the function until clearInterval() is called or the window is closed. The ID value returned by setInterval() can be used as an argument to the clearInterval() method.Copy the code