1. How many language types does JavaScript specify?
- Number
- String
- Boolean
- undefined
- Null
- Object
- Symbol
- Set
- Map
- BigInt (a built-in object that provides a way to represent integers greater than 2^ 53-1. This was originally the largest Number that could be represented by Number in Javascript. BigInt can represent any large integer.)
2. What is the underlying data structure of JavaScript objects and how they are stored in memory?
-
The basic types of JavaScript data are stored directly on the stack as values (Undefined, Null, non-new Boolean, numbers, and strings), and the size of the memory space for each type of data is determined and automatically allocated and freed by the system. The benefit of this is that the memory can be reclaimed in a timely manner and it is easier to manage the memory space than the heap.
-
JavaScript reference type data is stored in the heap (objects, arrays, functions, etc., which are copied and new). In fact, it is not quite accurate to say that the address pointer of the reference type is stored in the stack. When we want to access the value of the reference type, we need to get the address pointer of the object from the stack, and then use the address pointer to find the data in the heap.
3.Symbol type in the actual development of the application, can be manually implemented a simple Symbol.
(Note that you cannot use the new operator when creating a value using the Symbol() method, because the result of instantiating a new object is an object, not a Symbol of the original type.)
- In practice, when you encounter complex objects or object types that need to be maintained by multiple people, it is easy to create duplicate attribute names. Using symbol can avoid this problem:
const opt1 = Symbol('key');
const opt2 = Symbol('key');
const obj = {
[opt1]: 'value',
[opt2]: 'value',}// obj:{Symbol(key): "value", Symbol(key): "value"}
Copy the code
- The second case is to simulate private variables in a class
// a.js
const privateKey = Symbol('key')
class myClass{
constructor(){
this.name = 'test'
// Because of the unique nature of Symbol, the properties here can only be accessed in a.js.
// The new instance cannot access the privateKey.
this[privateKey] = 'strange'}}Copy the code
4. What built-in objects correspond to basic types and how they are boxed and unboxed?
For example, String, Number, Boolean, Object, Function, Array, Date, RegExp, and Error. These are actually built-in functions that can be used as constructors to create a new object of a subtype.
- Boxing refers to the operation of converting a basic data type to the corresponding reference type. And packing is divided into implicit packing and explicit packing.
- Unpacking is the opposite of packing. Convert a reference type to a basic data type. This is typically done by referring to the valueOf() and toString() methods of the type.
For details, see juejin.cn/post/684490…
The difference between null and undefined
-
Undefined literally means: undefined value. Returns when an attempt is made to read a variable that is undefined or defined without assignment.
-
The literal meaning of null is: null value. It can be represented in memory as a pointer address in the stack that does not point to a memory object in the heap. In the original implementation of JS, the value in was represented by a tag representing the type and the actual data value, and the type tag of an object was 0. Since null represents a null pointer 0x00, the type tag of null is 0, and typeof NULL returns “object” incorrectly.)
6. There are at least three ways to determine the JavaScript data type, and their advantages and disadvantages. How to accurately determine the array type?
- typeof
- Advantages: Simple to write and easy to understand
- Disadvantages: Can only distinguish between basic data types, cannot determine complex data types, such as NULL,Array,Date, etc. (valid function)
- instanceof
- Advantage: You can determine whether an instance is an instance of its parent or ancestor type
- Disadvantages: Left must be an object (null,undefined), error when prototype chain changes
- Object.prototype.toString.call
- Advantage: can output data type string directly, easy to judge, applicable to all data types
- Faults: Trouble writing
Judge: an Array Object. The prototype. ToString. Call (new Array ()) = = = ‘[Object Array]’
7. The reason for the loss of decimal precision, the maximum number that can be stored in JavaScript, the maximum security number, the method of processing large numbers in JavaScript, and the method of avoiding the loss of precision?
Cause: JS complies with IEEE 754 specification and uses double precision storage, occupying 64 bit. One bit is used for the sign bit, 11 for the exponent, and 52 for the mantissa.
Because at the bottom level of the computer, numerical operations and operations are implemented in binary, so the computer has no way to accurately express floating point numbers, and can only approximate binary equal to the decimal part of the floating point number.
0.1 >> 0.0001 1001 1001 1001... 0.2 >> 0.0011 0011 0011 0011... (0011) infinite loop turn the decimal 0.30000000000000004 0.1 + 0.2 = 0.01001100110011001100110011001100110011001100110011001110Copy the code
When performing a calculation or other operation, rounding (rounding every one, rounding every zero) will cause the final result to be biased.
The same problem exists with large integers, because the mantissa has only 52 bits, so the largest integer that JS can accurately represent is Math.pow(2, 53), which is 9007199254740992 in decimal. (ES2015 BigInt is used to solve large number problems)
The loss of precision can be solved by introducing a tripartite library, such as Math.js, that deals specifically with this problem.