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(5GetName () getName() Foo().getName() getName() getName()new Foo.getName()
new Foo().getName()
Copy the code

This question looks simple, but examines a lot of JS basic knowledge points

Foo. GetName () // 2 This should be unquestionable because Foo is not executed internally, so the result of executing outside is 2

GetName () // 4: ❓ = 5; getName() // 4: ❓ = 5; The function is declared and assigned (this is why everyday function declaration is written below, call is written above, also can be used), while var is only declared, but not assigned

console.log(a) // undefined 
var a = 1
console.log(a) / / 1
Copy the code

The answer to all these questions is 4

Foo().getName() //1 This should be unquestionable, the function executes, calls the getName method inside, so the result prints 1

GetName () // 1 Since Foo().getName() is executed above, the inside getName replaces the outside getName function to the global, all that is executed is the inside getName of Foo

New foo.getName () // 2 GetName () takes precedence over new, so there is a second call to foo.getName (), so 2. New is misleading

New Foo().getName() // 3: Foo().getname () New Foo() is given a higher priority by enclosing a parenthesis, which will first execute new Foo() and then call.getName(), so it’s not 1 because the inner getName is not bound to this, so it’s 3