javascript you don't know

1. Data Type

There are 7 data types in js

Six of them are basic types including null, undefined, boolean, number, string and symbol, and one is object

But when judging the data type, you can use typeof except for null

1. null

How should null be judged? It should be like this

typeof a === 'object' && !a

2) undefined

Undefined judgment can use typeof, but in js, undeclared and undefined are the same, so it is impossible to know whether the variable is declared or not.

Fortunately, if a variable is used undeclared, it will cause an error in strict mode, and judging an undeclared variable can prevent the error from being reported.

For example, to indicate that adding a is not declared, declare that an a can be written

 

2. Arrays and array-like

1. Array-like refers to a class of objects, they are iterable, have values ​​as properties, and the most important point is that they have a length property, so strings are also an array of classes.

2. The judgment of js array cannot use typeof, but there is a new judgment method instanceof for objects. 

3. An array is an object, so it can be written as a['foo'] = 'bar', but it can't increase its length, but the more interesting point is that a["13"] = "for", it will Causes length to become 14, although the incoming 13 is a value

4. Array-like conversion arrays can be used

 Array.from( fakeArr ) 

 

can also

Array.prototype.slice.call (fakeArr)

 

, Array.from is a pretty powerful thing, and slice is a more interesting implementation.

5. The method of borrowing an array is a very fun thing. In js, the array has a super rich master, there are many practical tools, and the string is very poor, so the string can be borrowed from the array, how to borrow it? , just like the call above.

 

3. Numerical value

Numerical knowledge is relatively unpopular, but also quite interesting 

1. What has been criticized all the time must be that there is no real integer in js, a very classic one is

 0.2 + 0.1 = 0.30000000000000004

 

It is because the internal storage of js is not an integer, and floating-point numbers are distinguished in this way.

 2. It is a very uncommon practice to use e to represent a value. It is estimated that it is only because the calculator display is not enough. It is also possible in js. For example, 3.04e3 is equal to 3040

 3. The interesting thing about numerical values ​​is that 42.toFixed(3) is a wrong syntax, because the dot after 42 is interpreted as a decimal point because 42. is also a correct expression, so 42..toFixed(3 ) is fine

 4. The hexadecimal value is preceded by 0x and the octal value is preceded by 0

 5. NaN does not mean that the data is not a numerical value, but rather a dead numerical value, such as 2/ 'a' is a dead numerical value. but

typeof 2/'a' === ‘number' // true

 

At the same time, there is a global function isNaN in js,  

'a' === NaN // false
isNaN('a') // true

 

 6. +0 and -0 are numerically the same in js, but different from a vector perspective.

 7. ES6's new method Object.is

Object.is(2/ 'a',  NaN) // true

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324665525&siteId=291194637