Preface:

Recently saw a big factory interview questions collection, here pick out some interesting interview questions to share with you, through the analysis of these questions, deepen our understanding of JS, tamp the basic knowledge.

1. Output the execution result of the following codes

var obj = {
  '2': 3,
  '3': 4,
  'length': 2,
  'splice': Array.prototype.splice,
  'push': Array.prototype.push
}
obj.push(1);
obj.push(2);
console.log(obj);

Copy the code

Site 1:

Understanding push, which determines where to start inserting a given value based on length.

Let’s strip out the rest of the code and just keep push to look at the console output

var obj = { '2': 3, '3': 4, 'length': 2, // 'splice': Array.prototype.splice, 'push': Array.prototype.push } obj.push(1); obj.push(2); console.log(obj); Output: {2: 1, 3: 2, length: 4, push: ƒ}Copy the code

As you can see, since length is 2, push starts at subscript 2, and the values at subscript 2 and 3 are overwritten by the values pushed in.

Next we change the length

var obj = { '2': 3, '3': 4, 'length': 0, // 'splice': Array.prototype.splice, 'push': Array.prototype.push } obj.push(1); obj.push(2); console.log(obj); Output: {0: 1, 1: 2, 2: 3, 3: 4, length: 2, push: ƒ}Copy the code

You can see that if length is changed to 0, then push is inserted from 0

Expedition 2:

When an object has the splice function as a property, the console prints it as an array

var obj = { '2': 3, '3': 4, 'length': 2, 'splice': Array.prototype.splice, 'push': Array.prototype.push } obj.push(1); obj.push(2); console.log(obj); Output: [empty × 2, 1, 2, splice: ƒ, push: ƒ]Copy the code

Although the console output is an array, obj is still an object type

var obj = { '2': 3, '3': 4, 'length': 2, 'splice': Array.prototype.splice, 'push': Array.prototype.push } obj.push(1); obj.push(2); console.log(obj); Log (obj instanceof Array) console.log(obj instanceof Object) Output: [empty × 2, 1, 2, splice: ƒ, push: ƒ] false trueCopy the code

Combining the two points, we can analyze the final result

[empty × 2, 1, 2, splice: ƒ, push: ƒ]
Copy the code

2. Output the execution result of the following codes

var a = {n:1};
var b = a; 
a.x = a = {n:2};
console.log(a.x);
console.log(b.x);
Copy the code

Site 1:

Consecutive assignments, from right to left.

Expedition 2:

Operator precedence, the precedence of the dot over the equal sign. For more operator priorities, see Operator Priorities

KaoChaDian 3

Values referencing data types are Pointers to the heap.

Let’s look at the output first

undefined
{n: 2}
Copy the code

Resolution:

var a = {n:1}; var b = a; {n:1} a.x = a = {n:2}; {n:1,x:undefined} // 2. Will a redirect to 2} {n: / / 3. Will {n: 1, x: undefined} orientation of x 2} {n: / / 4. B :{n: 1,x:{n:2}}, a: {n:2} console.log(a.x); console.log(b.x);Copy the code

3. Output the execution result of the following codes

var a = 10;
(function(){
 console.log(a);
 a=5;
 console.log(window.a);
 var a = 20;
 console.log(a)
})()
Copy the code

Site 1:

Variable promotion. Prior to ES6, there were only global and functional scopes. Variable promotion is the beginning of promoting the variable definition to the current scope.

Expedition 2:

Scope. Before ES6, only global scope and function scope exist. When assigning a variable or searching a variable, the current scope will be searched.

Resolution:

var a = 10; (function(){ console.log(a); a=5; console.log(window.a); var a = 20; Console. log(a)})() undefined 10 20 // 1. Var a = 20; The variable promotion is done first. var a = undefined; // 3. Console. log(a) = undefined // 3.console. log(window.a) = 10 // 4 Var a = 20; The a variable is searched in the current scope and is assigned the value 20 // 5 when found. Console. log(a) is printed, and the value of a is 20Copy the code

4. Under what circumstances will A print 1 in the following code?

var a = ? ; if(a == 1 && a == 2 && a == 3){ console.log(1); }Copy the code

Site 1:

Implicit type conversion, which is performed when +- or == is performed.

Expedition 2:

When an object is converted to a primitive type, its own valueOf method is called first. If no primitive value is returned, its own toString method is called. If no primitive type is returned, an exception is thrown.

var a = { i:1, toString:function(){ console.log('toString',this.i) return this.i++ }, valueOf:function(){ console.log('valueOf',this.i) return this.i++ } }; if(a == 1 && a == 2 && a == 3){ console.log(1); } Output: valueOf 1 valueOf 2 valueOf 3 1 // 1. a is an object, and valueOf // 2 will be called when valueOf is converted to a number. When the expression in () completes, the I in a becomes 4Copy the code

5. Output the execution result of the following codes

var b = 10;
(function b(){
  b=20;
  console.log(b)
})()
console.log(b)
Copy the code

Site 1:

Executing the expression immediately generates a local scope that acts as a quarantine for parameters.

Expedition 2:

Function promotion is higher than variable promotion example:

var a = 1; Function a(){} console.log(aCopy the code

Thus, functions take precedence over var variable definitions.

Expedition 3:

Named function expressions can only be accessed internally, and the name of a named function expression cannot be reassigned

Here’s an example:

var a = function b(){ console.log(b); b=1; console.log(); } Output: ƒ b(){console.log(b); b=1; Log.console.log (b)} ƒ b(){console.log(b); b=1; console.log(b) }Copy the code

Resolution:

var b = 10; (function b(){ b=20; The console. The log (b)}) () the console. The log output (b) : ƒ () {b b = 20. The console. The log (b)} 10 / / 1. There are two kinds of immediate execution function definition method (1). (the function () {}) () (2). (the function () {} ()) (3).! ,,, +, - and other symbols! Function (){}() {// 2. Function (){}(); // 4. B =20; // 4. The final console output is the global variable B, so 10Copy the code