Interview questions - function prototypes

Question about function prototype:
	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();
Analysis: var presence and anonymous functions to variable lift code execution is lifted to the top of the following functions;
getName = function(){
	console.log(5)
}
Function is executed
  1. Foo getName method of performing an output;
	Foo.getName() // 2;
  1. Is performed var getName = ... will function to enhance the coverage;
	getName() // 4
  1. After performing the function and cover getName Foo, then call getName function, then the function call getName;
	Foo().getName() // 1
  1. Performing getName () function is a step function is performed on the case;
	getName() // 1
  1. This function is performed investigation operator precedence ;
	new Foo.getName() // => new (Foo.getName)()  输出 2;
  1. This function performs the same inspection operator precedence , but this time the new new Foo () is executed example Foo Foo instance because there is no method getName, you will find the prototype to protoType;
	new Foo().getName() // => (new Foo()).getName() 输出 3
  1. This function performs the same inspection operator precedence , the first two are integrated;
new new Foo().getName() // => new ((new Foo()).getName)() // 3 
Published 58 original articles · won praise 20 · views 110 000 +

Guess you like

Origin blog.csdn.net/fly_wugui/article/details/105227671