Front-end interview questions (js part)

1. What is the difference between undefined and null?

undefined means that the definition is not assigned

let a; a is not assigned, at this time the type of a is undefiend

null is defined and assigned, but the value is null

let b = null; At this time the type of a is null

2. When do you assign null to a variable?

Initial assignment, indicating that the object will be assigned

let a = null;
a={name: “jack”};

Before the end, make the object garbage (collected by the garbage collector)

let a = null
a = {name:“zs”};
a =null

3. Strictly distinguish between variable type and data type?

type of data:

1. Basic type
2. Object type

Variable type

1. Basic type: save is the basic type of data
2. Reference type: save the address value (such as: ox123)

example:

let a = "abc";
"abc" is the basic type in the data type ;
variable a is the basic type in the variable type; saving is the basic type of data ("abc")

The same

let b = [1,2];
[1,2] is the object type in the data type . The
variable b is the reference type in the variable type , and it stores the address value
as the picture shows

4. Prototype

Question 1:

function A (){
    
    
}
A.prototype.n = 1
var b = new A()
A.prototype = {
    
    
n:2,
m:3
}
var c = new A()
console.log(b.n,b.m,c.b,c.m)
// 1,undefined,2,3

Question 2:

function F(){
    
    }
Object.prototype.a = function(){
    
    
console.log('a()')
}
Function.prototype.b = function(){
    
    
console.log('b()')
}
var f = new F()
f.a()
f.b()
F.a()
F.b()
//a()
//报错
//a()
//b()

Insert picture description here

5. Variable declaration promotion and function declaration promotion

Question 1:

 var a =  4;
       function fn(){
    
    
           console.log(a);
           var a = 5;
       }
       fn();

What is the final value of a? // undefined variable declaration promotion

Variable declaration promotion

  1. Variables defined (declared) by var can be accessed before the statement is defined
  2. Value: underfined
    Insert picture description here

Question 2:

	fn();
       function fn(){
    
    
           console.log(123);
       }

Insert picture description here
Functions can be accessed before they are defined

Function declaration promotion

  1. The function declared by function can be called directly before
  2. Value: function definition object
    Insert picture description here

note:

 fn();
       var fn = function (){
    
    
           console.log(123);
       }

This is the promotion of variable declarations, through var declaration is the promotion of variable declarations

6. Execution context stack

Question 1:

 console.log('global bengin:'+i);
   var i = 1;
   foo(1);
   function foo(i){
    
    
       if(i==4) return;
       console.log('foo() begin:'+i);
       foo(i+1);
       console.log('foo() end:'+i);
   }
   console.log('global end:'+i);
  1. What will be output in turn?
    Insert picture description here

  2. How many execution contexts are generated during the whole process?
    5 = foo(1)+f00(2)+foo(3)+foo(4)+window

Question 2:

 function a(){
    
    }
   var a;
   console.log(typeof a);

result:

Insert picture description here

analysis:

Insert picture description here
Function promotion has a higher priority than variable promotion, which means that it will be executed later, and the variable promotion will be executed first, and then the function promotion will be executed, and it will not be overwritten by the variable declaration. a is function promotion instead of variable promotion

Question 3:

if(!(b in window)){
    
    
       var b =1
   }
   console.log(b);
   

result:

Question 4:

 var c =1
   function c(c){
    
    
       console.log(c);
   }
   c(2) // 报错

result:

Insert picture description here

analysis:

c(2) c is a variable and not a function. Function promotion has a higher priority than variable promotion. It is executed after the representative. The variable promotion is executed first, and then the function promotion is executed. It will not be overwritten by the variable declaration, but will be Overwrite after variable assignment. So a is a variable promotion, so c is a variable and cannot be called, so an error will be reported
Insert picture description here

7. Job domain chain

Question 1:

 var x = 10;
      function fn(){
    
    
          console.log(x);
          
      }
      function show(f){
    
    
          var x =20;
          f();
      }
      show(fn);

result:

Insert picture description here

analysis:

Insert picture description here
There is no x variable in the scope of the fn function. If there is no variable, then look up (look for the global scope) and the
output x is 10;

Question 2:

在这里插入代码片  var fn = function(){
    
    
          console.log(fn);
      }
      fn()
      var obj = {
    
    
          fn2: function(){
    
    
              console.log(fn2);
              
          }
        }
        obj.fn2()

result:

Insert picture description here

analysis:

fn is a method under the global window, which can be found,
Insert picture description here

But fn2 is a method in the obj range, which can be accessed through this.fn2

  var fn = function(){
    
    
          console.log(fn);
      }
      fn()
      var obj = {
    
    
          fn2: function(){
    
    
              console.log(this.fn2);
              
          }
        }
        obj.fn2()

Insert picture description here

8. Closure

Question 1:

 function  fn(a=0){
    
    
        var num =223
        var fn1 = function(b=1){
    
    
            num+=b
            console.log(num);          
        }
        num+=a;
        return fn1
    }
    var fn2 = fn()
    fn(2)()
    fn(3)()
    fn()(2)

result:

Insert picture description here

analysis:

Every time fn() is called, a new closure is generated, and the value of the formal parameter is different

Question 2:

 function fn(n,o){
    
    
       console.log(o);
       return {
    
    
           fn:function(m) {
    
    
               return fn(m,n)
           }
       }
   }
   var a = fn(0); a.fn(1),a.fn(2),a.fn(3)
   var b = fn(0).fn(1).fn(2).fn(3);
   var c = fn(0).fn(1); c.fn(2);c.fn(3)

result:

Insert picture description here

Question 3:

var name = "text window";
   var obj = {
    
    
       name: "my object",
       getNameFunc: function(){
    
    
           return function(){
    
    
               return this.name;
           }
       }
   }
   console.log(obj.getNameFunc()());

   var name2 = "text window";
   var obj = {
    
    
       name2: "my object",
       getNameFunc: function(){
    
    
           var that = this
           return function(){
    
    
               return that.name2;
           }
       }
   }
   console.log(obj.getNameFunc()());

result:

Insert picture description here

If there is an error, please let me know!

Guess you like

Origin blog.csdn.net/yrfjygb/article/details/113745643