Interview questions collection

  1. Mainstream browsers and their kernels on the market.
    Browsers are divided into shell (shell style) and kernel.
    Insert image description here
    Here is a quote from an article by a big guy: Five major browsers and four kernels.

  2. In js, when the expression after the switch statement is compared with the expression after the case statement, the strict equality operator (= = =) is used instead of the equality operator (==) , which means that when comparing No type conversion occurs.

  3. In js, global variables created using var (essentially properties of window) cannot be deleted using delete, but global variables created without var (essentially properties of window) can be deleted using delete. Because the configurable attribute in the built-in properties of variables created using var defaults to false, they cannot be deleted.

  4. In js, the Boolean values ​​corresponding to empty arrays ([]) and empty objects ({}) are both true.

  5. There are no integers at the bottom of the JavaScript language. All numbers are stored in the form of 64-bit floating point numbers, even integers.

1 === 1.0 // true
0.1 + 0.2 === 0.3 // false
0.3 / 0.1 // 2.9999999999999996
(0.3 - 0.2) === (0.2 - 0.1) // false
  1. In js, note that for the var command, local variables can only be declared inside the function. If declared in other blocks, they are all global variables.
  2. In js, the function itself is also a value and has its own scope. Its scope is the same as that of a variable, which is the scope in which it is declared, regardless of the scope in which it is run.
var a = 1;
var x = function () {
    
    
  console.log(a);
};

function f() {
    
    
  var a = 2;
  x();
}

f() // 1
  1. Closures
    There are two biggest uses of closures. One is to read the variables inside the outer function, and the other is to keep these variables in the memory. That is, the closure can make the environment in which it was born always exist.
    Note** that every time the outer function is run, a new closure will be generated, and this closure will retain the internal variables of the outer function, so the memory consumption is very large. **Therefore, closures cannot be abused, otherwise it will cause performance problems on the web page.
  2. In js, it is worth noting that since an array is essentially an object, you can add attributes to the array, but this does not affect the value of the length attribute.
var a = [];

a['p'] = 'abc';
a.length // 0
a['p'] //abc

a[2.1] = '123';
a.length // 0
a[2.1] // 123
  1. There is a special application of "exclusive OR operation". Perform three consecutive exclusive OR operations on two numbers a and b, a^=b; b^=a; a^=b;, and their values ​​can be interchanged . This means that using the "XOR operation" can interchange the values ​​​​of two variables without introducing temporary variables.
var a = 10;
var b = 99;

a ^= b, b ^= a, a ^= b;

a // 99
b // 10
  1. In js, note that once the value function get (or value function set) is defined, the writable attribute cannot be set to true, or the value attribute can be defined at the same time, otherwise an error will be reported.
var obj = {
    
    };

Object.defineProperty(obj, 'p', {
    
    
  value: 123,
  get: function() {
    
     return 456; }
});
// TypeError: Invalid property.
// A property cannot both have accessors and be writable or have a value

Object.defineProperty(obj, 'p', {
    
    
  writable: true,
  get: function() {
    
     return 456; }
});
// TypeError: Invalid property descriptor.
// Cannot both specify accessors and a value or writable attribute

In the above code, if the get attribute and the value attribute are defined at the same time, and the writable attribute is set to true, an error will be reported.

  1. Number() 、String()、 Boolean()
var v1 = new Number(123);
var v2 = new String('abc');
var v3 = new Boolean(true);

typeof v1 // "object"
typeof v2 // "object"
typeof v3 // "object"

v1 === 123 // false
v2 === 'abc' // false
v3 === true // false
// 字符串转为数值
Number('123') // 123

// 数值转为字符串
String(123) // "123"

// 数值转为布尔值
Boolean(123) // true

To summarize, when these three objects are used as constructors (with new), primitive type values ​​can be converted into objects; when used as ordinary functions (without new), any type of value can be converted into primitive type of value.

  1. The hasOwnProperty method of an object instance returns a Boolean value, which is used to determine whether a property is defined on the object itself or on the prototype chain.
Date.hasOwnProperty('length') // true
Date.hasOwnProperty('toString') // false

The above code shows that Date.length (how many parameters the constructor Date can accept) is a property of Date itself, and Date.toString is an inherited property.

In addition, the hasOwnProperty method is the only method in JavaScript that does not traverse the prototype chain when processing object properties.

  1. The callback function of Promise is not a normal asynchronous task, but a microtask. The difference between them is that normal tasks are appended to the next round of event loop**, and microtasks are appended to this round of event loop. This means that microtasks must execute earlier than normal asynchronous tasks. **
setTimeout(function() {
    
    
  console.log(1);
}, 0);

new Promise(function (resolve, reject) {
    
    
  resolve(2);
}).then(console.log);

console.log(3);
// 3
// 2
// 1

The output of the above code is 321. This shows that the execution time of then's callback function is earlier than setTimeout(fn, 0). Because then is executed in this round of event loop, setTimeout(fn, 0) is executed at the beginning of the next round of event loop.

Guess you like

Origin blog.csdn.net/weixin_46683645/article/details/121517100