1. Data types

Null, undefined, boolean, number, string, symbol(es6),bigint(es6),object

1.1 number and bigint

number

Js stores number as a 64-bit floating point number (IEEE754)

The conversion rules are as follows:


6.5 = 110. 1 2 = 1.101 2 4 = 1 0 ( 1 + 0.101 ) 2 1025 1023 = ( 1 ) s i g n ( 1 + f r a c t i o n ) 2 e x p o n e n t 1023 6.5 = 110.1 _2 = 1.101 * 2 ^ ^ 0 = 4-1 * (1 + 0.101) * 2 ^ = {}, 1025-1023 (1) ^ {signs} * (1 + fraction) * 2 ^ {} exponent – 1023

So sign = 0, exponent = 1025 = 100000000012, fraction = 1010000.. 0002sign=0,exponent=1025=10000000001_2,fraction=1010000.. 000_2sign=0,exponent=1025=100000000012,fraction=1010000.. 0002

Why exponent−1023exponent −1023exponent −1023?

Because 211=20482^{11} = 2048211=2048 means the range is [0,2047][0,2047][0,2047] and the exponential part may be negative, Therefore, the range of e−1023e-1023e−1023 is converted to [−1022,1023][-1022,1023][−1022,1023], where e∈(0,2047)e \in (0,2047)e∈(0,2047), Therefore, the range of normal that JS can represent is [2−1022,21023][2^{-1022},2^{1023}][2−1022,21023].

When e = = = = 0 0 e e = = 0

  • f! =0f! =0f! =0: indicates subnormal, that is, a normal number smaller than 2−10222^{-1022}2−1022. The calculation method is as follows:

( 1 ) sign x 2 1 1023 x 0. fraction = ( 1 ) sign x 2 1022 x 0. fraction {\displaystyle (-1)^{\text{sign}}\times 2^{1-1023}\times 0.{\text{fraction}}=(-1)^{\text{sign}}\times 2^{-1022}\times 0.{\text{fraction}}}
  • F = = 0 f = = 0 f = = 0, 0 ∣ ∣ 00 – | | – 00 ∣ ∣ – 0, marks the direction of the trend

When e = = 2047 e = = e = 2047 = 2047


  • f = = 0 f == 0
    Said,Infinity– Infinity and * *

  • f ! = 0 f! = 0
    saidNaN

bigint

Bigint Es6 solving the number can only be accurately said [- (253-1), 253-1] [- (2 ^ {53} – 1), 2 ^ {53} – 1] [- (253-1), 253-1) between the integer problem.

When this range is exceeded, the following condition occurs for number

This is because the fraction is only 52 digits. When the fraction exceeds this range, the lower digit is dropped for lack of space.

1.2 null, and undefined

Undefined is automatically assigned by the compiler when a variable is declared and not assigned, and NULL requires manual assignment to indicate an object value that is intentionally missing

null= =undefined//true
null= = =undefined//false
typeof(null)//object
typeof(undefined)//undefined
Copy the code

The About typeof () :

First we know the value of js is stored with a type tag(at the lowest bits) in memory:

  • 000: object. The data is a reference to an object.
  • 1: int. The data is a 31 bit signed integer.
  • 010: double. The data is a reference to a double floating point number.
  • 100: string. The data is a reference to a string.
  • 110: boolean. The data is a boolean.

Two values were special:

  • undefined (JSVAL_VOID) was the integer
    2 30 – 2 ^ {30}
    (a number outside the integer range).
  • null (JSVAL_NULL) was the machine code NULL pointer. Or: an object type tag plus a reference that is zero. We can think of NULL as an object with zero references

Then the engine’s code for typeof:

When checking (1) undefined

  #define JSVAL_IS_VOID(v)  ((v) == JSVAL_VOID)
Copy the code

When (2)object, check the Type tag. Null Pointer is stored in memory as 0x000x000x00 and cannot be called, so it is judged as object

You can also get typeof output values:

  • number
  • string
  • boolean
  • undefined
  • object
  • function
  • symbol(es6)
  • Bitint(es6)

Supplement:

We often use if… else

if(obj){doSomething}
if(! obj){doOtherThing}Copy the code

Why not call null or undefined?

This involves true and false values and casts in JS

7 falsy in JS:

false(boolean),+0(number),-0(number),null(null),undefined(null),”(string),NaN(number)

All other values are true

The if statement determines whether the result of an expression is True or false, and if a non-Boolean type is passed in, it is cast to Boolean based on its True/false value

So we can use!! Obj is cast to Boolean based on true and false value properties

1.3 Symbol

Used to create anonymous and unique object keys

/ / create
let a = Symbol('this is a')// Description Optional
a.toString()//Symbol('this is a')
let b = Symbol('this is b')

/ / use
let obj = {
    [a]:foo,
    [b]:bar,
    c:"this is c"
}// You must use [], otherwise it is a string
obj[a]//foo, can only be accessed with [], because. It is treated as a string

//
Symbol.for('foo') = = =Symbol.for('foo')// Check if there is a Symbol for key === 'foo' globally, return it if there is, create and register it if there is not
Symbol.keyFor(a)//undefined returns the register key

/ / traverse
Object.getOwnPropertySymbols(obj);
//[Symbol(this is a),Symbol(this is b)]
/ / use the for... In can't get Symblo
Reflect.ownKeys(obj)
//[Symbol(this is a),Symbol(this is b),"c"]
Copy the code

1.4 the Object

Set

A set can store a unique value of any type, including raw values or object references, with a comparison method similar to ===, especially in NaN

/ / create
let s = new Set(a)let s1 = new Set([1.1.1.2.2.3.NaN.NaN])/ / [1, 2, 3, NaN]
//CRUD
s.add(1)
s.delete(1)
s.has(1)
s.clear()
/ / traverse
for (let item of s.values()){
    //do something
}
s.keys()
//set has no keys
s.values()
// You can also directly for... Of iterates over set because, like arrays, it is iterated by default
s.entries()
Copy the code

Both maps and filters of arrays can be used with sets, so simple implementations can merge sets

WeakSet

WeakSet can only store objects, which is not included in the reference of objects, so there is no memory leakage problem. It is suitable for temporarily storing some objects, such as DOM nodes: when these nodes are removed by documents, there will be no memory leakage.

At the same time, because of the unpredictable garbage collection mechanism, WeakSet cannot be traversed

// Any iterable initializes WeakSet as a parameter, and all members of that object (which are also required objects) automatically become members of weakSet instance objects.
const a = [[1.2], [3.4]].const ws = new WeakSet(a);
// WeakSet {[1, 2], [3, 4]}
//add,delete,has
Copy the code

Map

The built-in object is essentially a hash structure of key-value pairs, but the key value can only be a string.

Map keys can be of any type (including objects)

let obj = {o:"this is o"}
let map = new Map()
map.set(obj,"this is also o")// Returns the map itself
map.get(obj)// This is also o returns value
Copy the code

If the key is added repeatedly (based on the memory address), the key is overwritten.

WeakMap

WeakMap’s special case is that the object to which its key corresponds may disappear in the future.

A typical application scenario is when a WeakMap structure can be used to add data to a DOM element of a web page. When the DOM element is cleared, its corresponding WeakMap record is automatically removed.

Another use of WeakMap is to deploy private properties.

However, WeakMap weakly references only keys, not values. Key values are still normal references.