Interview Questions (1)

Updated continuously ~~~

1. The difference between GET and POST communication

  • Get requests can be cached, but Post requests cannot
  • Post is a little more secure than Get, because Get requests are contained in the URL and will be saved by the browser. Post is not, but in the case of packet capture, it is the same.
  • Post can use the Request Body to transfer more data than Get, which doesn’t have this technology
  • Urls have a length limit that affects Get requests, but this length limit is browser-specific, not RFC specific
  • Post supports more encoding types and does not restrict data types

2. What is the difference between SRC and href?

  • SRC is the point to the external resource, which is nested in the document where the current tag is; When the browser parses the element, it suspends downloading and processing of other resources.
  • Href refers to the location of the web resource (the hyperlink) and is used to establish a link to the current element or document. When the browser parses the element, it downloads the resource in parallel and does not stop processing the current document.

3. Three storage modes are commonly used in the front end

  • Cookies: advantages -(good compatibility, easy to request built-in cookies); Disadvantages -(size only 4K, automatic request headers add cookies waste traffic).
  • Localstorage: easy to operate, permanent storage (unless manually deleted), the size of 5M.
  • Sessionstorage: basically similar to localStorage, the difference is that sessionStorage will be cleaned up after the page is closed, and it cannot be shared in all same-origin Windows, is the callback level storage mode

4. Briefly explain the variable promotion problem in JS

The js engine works by parsing the code, retrieving all declared variables, and then running line by line.

Example:

console.log(a) 		// output: undefined

var a = 1

function b() {
	console.log(a)
}

b() 	// Output: 1
Copy the code

The actual order of execution of the above code looks like this:

Var a = undefined and a = 1; var a = undefined and a = 1;

var a = undefined

console.log(a)	 // undefined

a = 1

function b() {
	console.log(a)
}

b() / / 1
Copy the code

Step 2: Execute the code. Js is executed line by line from top to bottom, resulting in the current result, which is called a variable prompt.

5. Understand closures? Is the closure still you? What do closures do?

What are closures?

  • A closure is a combination of a function and the lexical environment in which it is declared.
  • Closures = function and sum of variables accessible within a function
  • The following is an example:
(function() {
	var a = 1;
	function add() {
		var b = 2
		var sum = b + a
		console.log(sum); / / 3
	}
	add()
	}
)()

// The add function itself and its internal accessible variable, a = 1, together are called closures.
Copy the code

What closures do:

The best use of closures is to hide variables. One of the features of closures is that the internal number of lines can always access arguments and variables declared in the external function. Based on this can be implemented private variables, privileged variables, stored variables.

6. JavaScript scope chain understanding

Nature: JavaScript creates an executable context during execution. The executable context contains references to the external lexical environment. Variables and declarations of the external lexical environment can be obtained through this reference.

Note: Scope chains can only be found from the inside out, not from the outside in. This is also the “closure” implementation principle. At this point, closures are not difficult to understand if you understand scope chains.

Step over to understand scope chains in JavaScript

7. What are the data types in JS?

Data types in JavaScript fall into two main categories: primitive types and complex (reference) types.

  • Primitive types: Boolean, undefined, NULL, number, string, symbol, BigInt(introduced in ES10)
  • Complex type: Object

What is the difference between null and undefined?

  • Null: indicates thatempty, indicates that there should be no value here; An object can be NULL, representing an empty object, and NULL itself is an object.

  • Undefined:There is noJavaScript is a dynamically typed language, and in addition to representing null values, members may not exist at all (because existence is only known at runtime), which is why undefined is useful.

9. Understanding of prototype chain?

A quick look at the prototype

var Person = function(msg){
	this.msg = msg
}

var person1 = new Person("xiaoming");

person1.constructor === Person	//true
Person === Person.prototype.constructor	//true
person1.__proto__ === Person.prototype	//true
person1.__proto__.contructor == person1.constructor	//true
Copy the code

The diagram is as follows:

  • Blue is the constructor
  • Green is the object from which the constructor instance comes
  • Orange is the constructor’s prototype, which is also the prototype of the object from which the constructor is instantiated

    Note the difference between prototype and __proto__. Prototype is a function property and __proto__ is an object property.

Cognitive prototype chain

The prototype chain for Person1 is shown above:

person1 —> Person.prototype —> Object.prototype —> null

In fact, a function is also an object, so the relationship of the prototype chain is a little more complicated

Person —> Function.prototype —> Object.prototype —>null

The clear diagram is as follows:

Note that all functions are function.prototype, including the Function constructor and Object constructor (highlighted in red).

10. Where are JavaScript primitive types and complex types stored

  • Base types are stored on the stack, but once referenced by a closure they become resident memory and are stored in the memory heap.
  • Complex types are stored in the memory heap.

What’s the difference between GET and POST?

  • Different data transmission modes: GET requests transfer data through URLS, while POST requests transfer data through request bodies.
  • The security is different: The data of POST is in the request body, so there is a certain security guarantee, while the data of GET is in the URL. Through the history, the cache can easily find the information.
  • GET refresh, back, and other browser action requests are harmless, and POST may submit the form repeatedly.

12. Both PUT and POST send new resources to the server. What is the difference?

  • The PUT method is idempotent, and one or more consecutive calls have the same effect (with no side effects), while the POST method is non-idempotent.
  • A PUT URI points to a specific single resource, whereas A POST can point to a collection of resources.