This is the 9th day of my participation in Gwen Challenge

1. This section describes the basic data types of JS.

There are six basic data types: Undefined, Null, Boolean, Number, String, Symbol (ES6) and BigInt (ES10).

Symbol represents a unique and immutable data type that has been created. I think it was developed to solve the problem of possible global variable conflicts.

BigInt is a numeric type of data that can represent integers in any precision format. Using BigInt, you can safely store and manipulate large integers, even if the Number is outside the safe integer range that Number can represent.

2. How many types of values does JavaScript have? Can you draw their memory map?

Knowledge points involved:

  • Stack: primitive data type (Undefined, Null, Boolean, Number, String)
  • Heap: Reference data types (objects, arrays, and functions)

The difference between the two types is that the storage location is different.

Primitive data types are directly stored in simple data segments in the stack, occupying small space and fixed size. They are frequently used data, so they are stored in the stack.

Reference data types Objects stored in the heap take up large space and are of variable size. If stored in the stack, it will affect the performance of the program; The reference data type stores a pointer on the stack to the starting address of the entity in the heap. When the interpreter looks for a reference value, it first retrieves its address in the stack and then retrieves the entity from the heap.

3. What is a heap? What is a stack? What are the differences and connections between them?

The concepts of heap and stack exist in data structures and operating system memory.

In the data structure, the access mode of data in the stack is first in last out. The heap is a priority queue and is sorted by priority, which can be specified by size. A complete binary tree is an implementation of a heap.

In an operating system, memory is divided into stack areas and heap areas.

The stack area memory is automatically allocated and released by the compiler to store function parameter values, local variable values and so on. It operates like a stack in a data structure.

Heap memory is usually allocated and freed by the programmer. If the programmer does not free heap memory, it may be reclaimed by the garbage collection mechanism at the end of the program.

4. What is the internal attribute [[Class]]?

All objects (such as arrays) whose Typeof returns “object” contain an internal attribute [[Class]] (we can think of this as an internal Class, rather than a Class in the traditional object-oriented sense). This property cannot be accessed directly, usually by the Object. The prototype. The toString ().. To look at it. Such as:

Object. The prototype. ToString. Call ([1, 2, 3]); // "[object Array]" Object.prototype.toString.call( /regex-literal/i ); // "[object RegExp]" // Classes created by ourselves do not have this special treatment, Class Class1 {} Class [[Class]] returns [Object Object] Class Class1 {} because toString() cannot find toStringTag. Object.prototype.toString.call(new Class1()); [[Class]] Class {get [symbol.tostringTag]() {return "Class2"; } } Object.prototype.toString.call(new Class2()); // "[object Class2]"Copy the code

5. What built-in objects does JS have?

Global objects, or standard built-in objects, are not to be confused with “global objects”. When I say global objects, I mean objects in the global scope. Other objects in the global scope can be created by the user’s script or provided by the host program.

Classification of standard built-in objects

(1) Value properties. These global properties return a simple value that has no properties or methods of its own.

Examples are Infinity, NaN, undefined, and null literals

(2) Function attributes: The global function can be called directly without specifying the object to which it belongs. The result will be returned to the caller directly after the execution.

Examples include eval(), parseFloat(), parseInt(), and so on

(3) Basic objects, which are the basis for defining or using other objects. Basic objects include generic objects, function objects, and error objects.

For example Object, Function, Boolean, Symbol, Error, etc

(4) number and date objects, used to represent numbers, dates, and objects to perform mathematical calculations.

Examples are Number, Math, and Date

(5) strings, objects used to represent and manipulate strings.

For example, String, RegExp

(6) indexable collection objects, which represent collections of data sorted by index value, including arrays and type arrays, and objects with array like structures. Such as Array

(7) Collection objects that use keys. These collection objects use keys when storing data, and support iterating elements in the order of insertion.

Such as Map, Set, WeakMap, WeakSet

(8) Vector set: The data in SIMD vector set will be organized into a data sequence.

Such as SIMD etc.

(9) Structured data. These objects are used to represent and manipulate structured buffer data, or data encoded using JSON.

Such as JSON,

(10) Control abstract objects

Examples include Promise, Generator, etc

(11) Reflection

For example Reflect, Proxy

(12) Internationalization, adding ECMAScript objects to support multilingual processing.

For example, Intl and intl.collator

WebAssembly (13)

(14) Others

For example the arguments

6. The difference between undefined and undeclared?

A variable declared in scope but not yet assigned is undefined. In contrast, variables that have not been declared in scope are undeclared.

ReferenceError: b is not defined However, we can use Typeof’s security precautions to avoid errors because Typeof returns “undefined” for undeclared (or not defined) variables.

7. Null vs. undefined

First, Undefined and Null are both basic datatypes, and each of these basic datatypes has only one value, namely Undefined and Null.

Undefined means undefined, and null means empty objects. Undefined is returned when a variable is declared but not yet defined. Null is used to initialize variables that may return objects.

Undefined is not a reserved word in JS, which means we can use undefined as a variable name, which is very dangerous and can affect our judgment of undefined. But there are ways to get a safe undefined value, such as void 0.

Null typing returns “object” when we use Typeof to judge two types, which is a relic of history. Returns true when we compare two types of values using a double equal sign, false when we compare two types of values with three equal signs.

8. How to obtain the safe value of undefined?

Because undefined is an identifier, it can be used and assigned as a variable, but this will affect the normal judgment of undefined.

The expression void ___ returns no value, so undefined is returned. Void does not change the result of the expression; it just makes the expression return no value.

By convention we use void 0 to get undefined.

9. What are some basic rules for writing JavaScript?

In normal project development, we follow some basic rules such as:

(1) a function scope of all variable declarations should be mentioned function first, use a var statement, not allowed two consecutive var statement, stating if the variable has no value, should give the initial value of the variable assignment of the corresponding type, facilitate others when reading code, can be clear at a glance know variables corresponding to the types of values.

(2) The address, time and other strings in the code need to be replaced by constants.

(3) Use ‘===’, ‘! ==’ instead of ‘==’, ‘! = ‘.

(4) Do not add methods on the prototype of built-in objects, such as Array and Date.

(5) Switch statements must have a default branch.

(6) The for loop must use braces.

(7) If statements must use braces.

10. JavaScript prototype, prototype chain? What are the characteristics?

In JS we use constructors to create objects. Each constructor has a prototype property value inside it, which is an object containing properties and methods that can be shared by all instances of the constructor. When we create an object using the constructor, the object contains a pointer to the constructor’s prototype property. In ES5, this pointer is called the prototype of the object. Normally we should not be able to get this value, but browsers now implement the proto property to give us access to this property, but it is best not to use this property because it is not specified in the specification. New to ES5 is the object.getProtoTypeof () method, which can be used to retrieve the prototype of an Object.

When we access a property of an object, if that property doesn’t exist inside the object, then it will look for that property in its prototype object, and that prototype object will have its own prototype, and so on and so forth, the concept of prototype chain. The end of the prototype chain is usually Object.prototype, so that’s why our new Object can use methods like toString().

Features:

JavaScript objects are passed by reference, and each new object entity we create does not have a copy of its own prototype. When we modify the stereotype, the objects associated with it inherit the change.

11. How does JS get the prototype?

  • p.proto
  • p.constructor.prototype
  • Object.getPrototypeOf(p)

12. The representation of different base numbers in JS

  • The value starts with 0X or 0X in hexadecimal notation.
  • Numbers starting with 0, 0O, and 0O are octal.
  • The value starting with 0B or 0B is in binary format.