The code is as follows :

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(); //2 => new (Foo.getName)() new No parameter list
new Foo().getName(); // 3 => (new Foo()).getName(); //new With parameter list
new new Foo().getName(); //3 => new ((new Foo()).getName)() //new With parameter list

Foo.getName()

The output is :2
principle : What he's calling is Foo Static method of ( If C#、java And so on ), Because Javascript Medium Function Itself is Function and Object The combination of , therefore function It's a function , Also object . ad locum Foo It's the execution of one of his objects function nothing more .

getName()

The output is :4
principle : Maybe you wonder why not 5,function The variable is raised to the top of the scope , and var Defined will not , therefore var The statement's override function Statement . So the result is 4

Foo().getName()

The output is :1
principle :Foo() It's a function call ,Foo In the executor getName Reassigned ( Overall getName, Look up... Based on the scope chain ), Back to this by window(Foo There is no call to , The default is global ), then .getName() Called the global getName( It means being Foo I wrote getName), So the result is :1

getName()

The output is :1 principle : because Foo In the executor, the global getName Rewrote , At this time, the call is global getName. So the result is :1

new Foo.getName()

The output is :2
principle :

  • because new No arguments , And new The operation optimization level with parameters is the same .new There is participation in . Operators at the same level , and new A lower level of nonparametric . So it's going to execute first . Operator to Foo Static method of getName
  • And then execute new, What you get is Foo.getName An example of . So the output is :2

new Foo().getName()

The output is :3

principle :

  • because new It's involved , And . Operator siblings , In order of execution from left to right , Execute first new Foo().
  • new Foo() It needs our attention , Because he has a return value . There are two types of return values :
    • No return has the same effect as return as a basic type : Return all instance objects ( It's the current this)
    • Return to reference type : The object returned is of this reference type , The instance object is replaced ( It's the current this No return ). Factor new Foo() The return is the instance object
  • .getName(): Select this method for the previous instance , So the output is :3

new new Foo().getName()

The output is :3
principle : Mainly is the operator priority examination , It should not be directly used in case development . First new Ginseng , then .getName(. Operator )( Why . Operator , This is because new The level of no parameter is one grade lower ), Again new Ginseng .

remember Javascript More related articles about the understanding of a problem

  1. Front end weekly : Front end interview questions and answers summary ;JavaScript Deep understanding of parameter passing

    1.2017 Front end interview questions and answers summary | Gold digger technical essay " Kim San Yin four , Golden nine silver ten ", It's used to describe the best months for a job . But as the industry saturates , The beginning, middle and front end er The employment situation is not optimistic . The state of the industry is out of control , We can do it ...

  2. 03.JavaScript Object oriented Essentials -- Understand the object

    JavaScript Object oriented Essentials -- Understand the object Even though JavaScript There are a lot of built-in reference types , It's very likely that you will still create your own objects frequently .JavaScript Objects in are dynamic . One . Defining attributes When an attribute number 1 Time ...

  3. JavaScript Can I multithread ? In depth understanding of JavaScript Timing mechanism ( Reprint )

    explain : Recently written js Need to use setinterval Function to do timing operation , Who knows , It's working well at the beginning , But after a while, he's gone , The interval between scheduled tasks is getting shorter and shorter , The frequency is increasing , It's a totally intolerable issue , With one that can go out ...

  4. JavaScript Closure understanding in

    Original article , Reprint please indicate :JavaScript Closure understanding in   By Lucio.Yang 1.JavaScript Closure In primary school development projects , use node.js Developed the server , In the process, I met node.js Of the ...

  5. JavaScript Some understanding of closures

    original text :JavaScript Some understanding of closures To put it simply : Closures are functions that can read internal variables of other functions . How to read the internal variables of other functions , We all know that JavaScript An inner function in can access variables in its parent function , that ...

  6. Yes JavaScript Understanding of closure in

    Closure is a very important knowledge point in front-end development , It's something that will be asked in the interview . My main understanding of closures is " Through closures, variables inside functions can be accessed outside functions ", There is also little use of closures , I even wrote the closure myself ...

  7. javascript in (function(){})(); How to understand ?

    javascript in (function(){})(); How to understand ? javascript in : (function(){})() It's an anonymous function , We mainly use the variable scope in the function , Avoid global variables , Affect the overall page ring ...

  8. turn : Yes JavaScript Understanding of closure in

    About  const     let      var  summary : It is recommended to use let  , Instead of using var, If you want to declare constants , Then use const. ES6(ES2015) Before appearance ,JavaScript The only variables declared in are ...

  9. javascript Understanding of anonymous functions ( turn )

    Original website http://www.jb51.net/article/21948.htm javascript Understanding of anonymous functions ( Thorough Edition ) The code is as follows : (function(){ // Here to ignore jQuery All reality ...

Random recommendation

  1. monitor Windows Shear plate

    One . Look at the code first import win32con,win32gui import win32clipboard as cb class MyWindow(): def __init__(self): # notes ...

  2. MYSQL Related complete notes

    useradd mysql -s /sbin/nologin cat/etc/passwd | grep mysqlcat /etc/group | grep mysql Source directory cd /usr/sr ...

  3. 【 Reprint 】Mysql View connections , state

    Reprinted address :http://blog.csdn.net/starnight_cbj/article/details/4492555 command : show processlist;  If it is root Account number , You can see ...

  4. Reading notes ——Windows Programming core (8)Interlocked One way chained stack

    SLists Lock free algorithm is used to ensure atomic synchronization , To improve system performance , Avoid problems such as priority hanging and interlock . Be careful : All linked list entries must be aligned to MEMORY_ALLOCATION_ALIGNMENT. Otherwise, there will be wonderful mistakes . ...

  5. mootools Inside $,$$,$E,$ES And so on

    The difference is $ and $$ All are 1 Parameters , $ Apply to ID, perhaps ID Represented by $$ Apply to CSS Selectors $E and $ES, Yes 2 Parameters , The second parameter is optional and represents (filter, That is, a certain one. ID The elements in the scope ) $E('inpu ...

  6. Python Basic course List object turn

    Python Basic course List object Time :2014-01-19    source : Server home    contribute :root   1.PyListObject object typedef struct {    PyObjec ...

  7. servlet Gain in Li jsp In the page select The value of the option

    <span style="font-size:24px;"><select name=first> <option value="1&quo ...

  8. sketch KVM The architecture and Xen framework

    Last summer update , because ,,, I'm going back to school tomorrow . The following is my personal understanding , If there's something wrong, I hope you can dalao generous with your criticism . virtualization Virtualization is through Hypervisor Program implementation ,Hypervisor The function of virtual machine is to make the hardware virtual ...

  9. LeetCode(115): Different subsequences

    Hard! Title Description : Given a string  S  And a string  T, Calculated at  S  In the subsequence of  T  Number of occurrences . A subsequence of a string refers to , By deleting some ( It can also be done without deleting ) A new word composed of characters without interfering with the relative position of the remaining characters ...

  10. VS2015+MySql+EF6 Summary of pit experience

    background :VS2015+MySql+EF6(DB First) Pit sequence : According to the memory of the past , The operation sequence is as follows : 1, install MySQL Connector/NET( Don't think about it , Install the latest ,8.0.12) 2. install  My ...