JS comprehensive face questions 1

function foo(){
getName = function () { alert(1); };
return this;
}
Foo.getName = function(){ alert(2); };
Foo.prototype.getName = function(){ alert(3); };
var getName = function(){ alert(4); };
function getName(){ alert(5)};

// Please write the series of output
Foo.getName ();
getName ();
Foo () getName ();.
GetName ();
new new Foo.getName ();
new new Foo () getName ();.
New new new new Foo () .getName ();

----analysis------

Before the function call //
1, the variable lift
to lift var getName; variable, the lifting function getName () {alert (5 );}, the variable name and function name conflict, the variable names are ignored.
2, to perform the function getName = function () {alert ( 4);} when rewriting the global method getName final getName = function () {alert ( 4);}

// call the following function execution
Foo.getName (); // 2
getName (); //. 4
Foo () getName ();.. 1 // The sentence can be written as ((Foo ()) getName. ) (), Since Foo () method to modify the global getName, so getName = function () {alert ( 1);}; and Foo () returns the value window, so the final window.getName () to alert a 1
// priority highest, perform
getName (); //. 1
new new Foo.getName (); // new new 2 (Foo.getName) () -> new new (function () {Alert (2);}) ()
new new Foo ( ) .getName (); // 3 ( new Foo ()) getName () -> foo.getName () call instance (prototype) method.
new new new new Foo () getName ();.. 3 new new // ((new new foo ()) getName) () -> new (foo.getName) () -> new (function () {alert (3);}.) ()

Guess you like

Origin www.cnblogs.com/mengzhongfeixue/p/11930558.html