bigInt; instanceof和typeof

A, bigInt:

 BigInt is a new data type, for, when the integer value is greater than the range of data types supported Number of time. This type of data allows us to safely perform arithmetic operations on large integer representing the high-resolution time stamps,

Use large integer id, etc., without the need to use the library to handle large integers.

 

Js is the only safe Number showing type (- (2 ^ 53 - 1)), to the ((2 ^ 53-1)), and any integer values ​​outside this range are likely to lose precision. Therefore, some security problems.

 

 

How to create and use BigInt:

Created: n only need to append to the end value.

console.log (9999999999999999999999999999n);

console.log(9999999999999999999999999999);

 

Another way to create: BigInt ( '99999999999999999999999');

 

Simple to use:

10n+20n  // ->  30n

10n-20n // -> -10n

+10n //  ->typeError

-10n //  -> -10n

10n * 20n  // -> 200n

20n / 10n // -> 2n

23n % 10n  // -> 3n

10n ** 3n // -> 1000n

 

const a = 10n;

++x; // ->  11n;

--x; //  9n

console.log(typeof a);  // ->  "bigint"

 

From the above examples may be given to know + 10N, since the particularity + symbol, when used with respect to all types of parameters with the + sign, is the equivalent to the number value in addition to do;

The + new Date () can get the current timestamp: new Date () will be converted to Number;

Therefore 10n + 10n should be converted to number, but because of this implicit type conversions can lose information, it is not permitted to convert between bigInt and number. It will be reported typeerror.

As follows: 10 + 10n will react typeerror, math.max (2n, 4n) will react typeerror (max required function is passed the parameter number).

 

 

When bigInt boolean type and meet, with the number of treatment as bigInt, i.e. 0n addition, other bigInt are ture.

if(3n) {}  // -> true

if(0n) {}  // -> false

 

 

 

 

 

Second, the use typeof instanceof determines the data type and

1.typeof:

First, underlying data type is determined using typeof:

typeof 1  // 'number'

typeof '1'  // 'string'

typeof undefined  // 'undefined'

typeof true  // 'boolean'

typeof Symbol() // 'symbol'

typeof 10n // 'bigInt'

typeof null // 'object'  

Conclusion -> In addition to basic data types null, other underlying data type can call typeof display the correct type.

 

However, a reference to a data type, in addition to the function, are displayed 'object',

 

typeof []  // -> 'object'

typeof {} // -> 'object'

typeof console.log // 'function'

 

Thus using typeof target data type is not appropriate, it will be better using instanceof, instanceof principle is based on the query prototype chain, as long as

Chain in the prototype, is determined is always true.

For all basic data types, using instanceof return value is false. Thus use can be very good to judge instanceof basic data types.

Guess you like

Origin www.cnblogs.com/kkkwoniu/p/12057316.html