1. Why typeof null === ‘object’?
typeof null // 'object'
Copy the code
Due to JavaScript, the value of a variable is stored in a 32-bit memory cell. The unit contains a 1 – or 3-bit type flag and the value of the actual data. The type flag is stored at the end of the cell.
-
000: object indicates the object
-
1: indicates an int – integer
-
010: a double-floating point number
-
100: string – A character string
-
110: Boolean – Boolean value
-
undefined -2^30
-
Null null pointer (all zeros)
The result is obvious, since the last three digits of null’s storage unit (all zeros) are exactly the same as object’s.
2. Does the equation 0.1 + 0.2 === 0.3 hold?
0.1 + 0.2= = =0.3 // false
Copy the code
Since 0.1 and 0.2 in binary floating-point numbers are not very accurate, they will be converted to binary first when the two numbers are added together. When 0.1 and 0.2 are converted to binary, the mantisca will occur an infinite loop, and then carry out logarithmic calculation. JS engine truncates binary, resulting in the loss of accuracy. So instead of just adding up to 0.3, they add up to a very, very, very close number: 0.300000000000000004, so the condition is false.
3. A ==1 &&a ==2 &&a ==3
1 / / method
var a = {
value: 1.valueOf: function () {
return this.value++; }};2 / / method
var a = {
value: 1.toString: function () {
return this.value++; }};3 / / method
var value = 1;
Object.defineProperty(window."a", {
get: function () {
return this.value++; }});if (a === 1 && a === 2 && a === 3) {
console.log("Isn't that amazing?")}Copy the code
Methods 1 and 2: JS objects have toString() and valueOf() methods. ToString () returns the original valueOf the object as a string, and valueOf() returns the original value that best fits the object
1. ValueOf () takes precedence over toString() when converting objects using operators
2. ToString () is preferred for strong string conversions to objects
3. The toString() method cannot convert null and undefined. The toString() method can be used instead
Method 3: Hijack variable A using Object.defineProperty() and return the value of variable A ++ in GET.
4. String reversal (‘ ZBW ‘->’ WBZ ‘)?
var name = 'zbw'
var res
res = name.split(' ').reverse().join(' ')
console.log(res) // 'wbz'
Copy the code
The string is converted to an array, then the array’s reverse method is called to reverse the array elements, and finally the array is converted back to the string
5. The function outputs 1,2,3,4,5 per second… 9?
for(var i = 0; i < 10; i++) {
setTimeout(function timer() {
console.log(i)
}, i * 1000)}Copy the code
Expectations: 1, 2, 3, 4, 5… 9
Result: Prints 10 every second
- Using the IIFE
for(var i = 0; i < 10; i++) {
(function(i) {
setTimeout(function timer() {
console.log(i)
}, i * 1000)
})(i)
}
Copy the code
- Let the keyword
for(var i = 0; i < 10; i++) {
let j = i // Block scope for closure
setTimeout(function timer() {
console.log(j)
}, j * 1000)}Copy the code
- Let keyword (recommended)
for(let i = 0; i < 10; i++) {
setTimeout(function timer() {
console.log(i)
}, i * 1000)}Copy the code
6. Say something about the orientation of this?
To determine the this binding of a running function, you need to find where the function is called (combined with the call stack), and then determine the this binding object based on four rules derived from its priority.
-
Function called by new? Bind to the newly created object
-
By call/apply/bind? Binds to the specified object
-
Called by a context object? Binding context object
-
Default: bind to undefined in strict mode, otherwise bind to global object
The arrow functions of ES6 do not apply the above four rules. Instead, the arrow function determines the this binding based on the current lexical scope. That is, the arrow function inherits the this binding of the outer function call (whatever it is bound to), and the arrow function’s this binding cannot be modified.
7. JavaScript data types?
Number string Boolean undefined NULL Object symbol bigInt
In addition to object, other types are collectively referred to as basic data types.
The object type is called a reference data type (complex data type) and contains two subtypes (array, function)
8. What does the Symbol type do?
-
Can be used to represent a unique variable, preventing naming conflicts.
-
Can be used to simulate private variables. (the method of using symbol are not regular (in addition to the Object. GetOwnPropertySymbols) traversal)
-
Mainly used to provide traversal interface, layout of symbol. Iterator objects can use for···of cycle, can be unified processing data structure.
9. What is NaN? Typeof NaN output?
NaN (not a number) is not a number, but typeof NaN prints ‘number’. In other words, NaN can be understood as a number that is not a number (albeit a bit of a mouthful).
Implicit conversion of JavaScript?
In general, valueOf() is called first for data of a non-basic data type, and toString() is called to retrieve the valueOf the basic data type if it cannot be retrieved.
- Add strings and numbers
If one of them is a string, then both are converted to strings and string concatenation is performed
'11' + 23 + '24' / / 112324
Copy the code
- Subtract a string from a number
Convert it to a number and run it
'11' - 2 / / 9
Copy the code
- Boolean values and numbers
Convert it to a number and run it
1 + true / / 2
1 + false / / 1
Copy the code
- Other types and Boolean types
Converts a Boolean type to a number and evaluates it
- Objects and non-objects
Execute the object ToPrimitive(valueOf/toString) and then compare