Summary of test questions (continuous update~~~)

Summary of test questions (continuous update~~~)

Summary of test questions

Summary of daily test questions to consolidate basic knowledge.

Test one

Test points: variable promotion, scope

var o = {
  fn: function(e) {
    console.log(e)
    console.log(fn)
  }
}

o.fn(1)
var fn = function(e) {
  console.log(e)
  console.log(fn)
}
fn(1)
 

The above code can be equivalent to the following code

var fn

var o = {
  fn: function(e) {
    console.log(e)//1
    console.log(fn)//undefined
  }
}

o.fn(1)

fn = function(e) {
  console.log(e)//1
  console.log(fn)//fn( )
}

fn(1)
 
  1. When o.fn(1) is executed, the formal parameter e is 1, and the output is 1. The fn knowledge statement is not defined, so the output is undefined
  2. fn(1), the formal parameter e is 1, the output is 1, fn is the global fn

Question two

Test points: scope, global variable leakage, this binding, new, variable promotion

function Foo() {
  getName = function() {
    console.log(1)
  }
  return this
}

Foo.getName = function() {
  console.log(2)
}

Foo.prototype.getName = function() {
  console.log(3)
}

var getName = function() {
  console.log(4)
}

function getName() {
  console.log(5)
}

Foo.getName()
getName()
Foo().getName()
getName()
new Foo.getName()
new Foo().getName()
new new Foo().getName()
 
Foo.getName()

Foo.getName() is easier to understand. The function is an object. Foo.getName defines the property getName on Foo, so it directly outputs 2

getName

getName() here involves variable promotion, the above code is equivalent to the following form:

function getName(){
  console.log(5)
}
var getName
getName = function() {
  console.log(4)
}
 

So this code will output 4

Foo().getName()

Foo().getName() execution here will cause global variables to leak, this binding

 function Foo() {
  getName = function() {
    console.log(1)
  }//  
  return this//this 
}

Foo().getName()//1
 

Since Foo() does not use var to define getName, getName will leak in the global (window.getName). At this time, the binding rule of this is the default rule (bound in the global), so Foo().getName() is equivalent On window, getName()

getName()

According to the analysis in the third step, the output of getName() is 1 at this time

new?

To figure out the following things, you must know what new did.

  1. Create (or construct) a brand new object.
  2. This new object will be executed [[prototype]] connection.
  3. This new object will be bound to the this of the function call.
  4. If the function does not return other objects, the function call in the new expression will automatically return this new object.
new Foo.getName()

After new is followed by an ordinary function, this function will execute normally, and the only side effect is that it will return a new object. So the output here is exactly the same as executing Foo.getName() directly, so the output is 2.

new Foo().getName()

It is equivalent to the following form:

var obj = new Foo()
obj.getName()
 

So here getName() will find Foo.prototype.getName on the prototype chain, so the output is 3.

It is necessary to say why Foo.getName() is not executed here

function Foo() {
  getName = function() {
    console.log(1)
  }
  this.getName = function() {
    console.log('getName')
  }
  return this
}
 

After the third step of new, you did not add the method getName to the newly created object, so you will use the getName on the prototype chain

new new Foo().getName()

It is equivalent to the following form:

var obj = new Foo()
var obj2 = new obj.getName()
 

Equivalent to new Foo.prototype.getName() so the output is 3

Test Question Three

function fun(n,o) {
  console.log('A', n)
  console.log('A', o)
  return {
    fun: function(m) {
      console.log('B', m)
      console.log('B', o)
      return fun(m, n)
    }
  }
}

fun(0).fun(1)