1, typeof, instanceof
The only purpose of typeof is to detect data types. When we determine reference data types, Object (Object, Array, Map, Set, etc.) is detected as Object, and instanceof is introduced for this purpose
Insatnceof method used to determine which constructor the reference data type belongs to
var arr = []
arr instanceof Array // true
Copy the code
Instanceof internal implementation principle:
The instaceof return value is determined by determining whether the object’s prototype can be found on the object’s prototype chain
Why is null incorrectly identified as “object” by typeof:
In the original implementation of JavaScript, values in JavaScript were represented by a tag representing a type and actual data values. The type label of the object is 0. Since null represents a null pointer (0x00 on most platforms), null’s type label is 0 and typeof NULL therefore returns “object”.
2. What is the result printed by the console?
for (let i = 0; i < 2; i++) {
setTimeout(function () {
console.log(i) // 0 1
}, 100);
}
for (var i = 0; i < 2; i++) {
setTimeout(function () {
console.log(i) // 2 2
}, 100);
}
Copy the code
Js is a single thread, setTimeout is an asynchronous macro task, and the code execution is put in the event queue when it is asynchronous, and the task in the event queue will be executed after the task in the thread is completed. Let is the way to declare variables in ES6, and it has its own scope block, which can put variables, so when let is bound to the for loop, Each I has its own value. In this for loop, an event that prints I is added to the event queue once, and each event has its own value for I. Var has no scope block, so the variables of the for loop will overwrite the previous one. When the loop is finished, I will have only one value, and because the judgment condition of the for loop does not meet the jump, I will be 2 instead of 1
var a, b; (function () { console.log(a); // undefined console.log(b); // undefined console.log(b); // undefined console.log(b); Var a = b = 3; var a = b = 3; // define a local variable a = 3 and assign the global variable b = 3 console.log(a); // The third output, local variable 3 console.log(b); // Fourth output, global variable 3})(); Console.log (a); console.log(a); console.log(a); // Global variable a = undefined console.log(b); // The sixth global variable b = 3Copy the code
Var a = b = 3, a is local, b is global
Var a = [1, 2, 3]; var b = a.slice(); / / b: [1, 2, 3] p. ush (4); // b: [1,2,3,4] console.log(a)Copy the code
The slice() method takes a portion of an array and returns a new array, so it doesn’t matter if it’s followed by a
function out(x) { var temp = 2; function inside(y) { document.write(x + y + (temp--)) } inside(5); } out(3); X: 3 y: 5 Temp: 2 X + y + (temp--) Temp -- is executed after the calculation. Therefore, x + y + temp => 3 + 5 + 2 = 10Copy the code
function foo() { console.log('first') setTimeout(function () { console.log('second') }, 5) } for (var i = 0; i < 4399; I++) {foo()} {foo() {foo()} {foo() {foo()} {foo() {foo()}} So this code will execute console.log('first') in the thread before console.log('second') in the task queue is executed.Copy the code
Var a = 2000 < 0 | | typeof (2000 + ") to the console, log (a) / / string is true if the first item was the result is true, if the first item to false, the results for the secondCopy the code
console.log(1); let a = setTimeout(() => { console.log(2) }, 0); console.log(3); Promise.resolve(4).then(b => { console.log(b); clearTimeout(a); }); console.log(5); // 1 3 5 4 Execute synchronous tasks first. - Output 1 3 5 and then execute asynchronous tasks. The asynchronous task is divided into macro task and micro task. The micro task takes precedence over the macro task, promise.then executes the micro task and outputs 4. Then clearTimeout(a) clears the timer and no longer prints 2Copy the code
var user = { count: 1, getCount: function () { return this.count; }} var func = user.getcount console.log(func()) // undefined 1, Var user = {} console.log(user.name) // undefined 2. Var one is undefined for basic types that are declared only without assigned values Console. log(two) // two is not defined in this case, this refers to window, Var func = user.getcount = function () {return this.count}; var func = function () {return this.count}; So this refers to the windowCopy the code
var foo = function (x, y) { return x - y; } function foo(x, y) { return x + y; } var num = foo(1, 2); // -1 This problem is processed by the JS compiler as: var foo var num function foo (x, y) { return x + y } foo = function (x, Y) {return x - y} num = foo(1,2) {return x - y} num = foo(1,2) {return x - y} num = foo(1,2)Copy the code