See a JS code, do not understand. Please advise?

  node.js, question

The place of the two arrows above is What Do You Mean? The prototype of the function in Kodo’s prototype is assigned to Kodo’s prototype?

Don’t understand ..

First of all, this code closely imitates jQuery. Kodo inside is equivalent to $,jQuery.

First arrow:Usually when we take a certain function A as a class, when we generate its instance, we are all directly new A. But jQuery
The design concept of is annoyingnewOf this keyword. In other words, you should have used:

div = new $('.box')

To get an element, but jQuery omitted new in order to make the API easy to use. This leads to the need to explicitly return a new object in the Kodo function. So, who should new be?return new Kodo(...)Certainly not, because it will lead to a dead cycle. Therefore, only one function can be newly constructed as a constructor to new, which is the code:initMethods. This method must have a place to put it. Yes, it isKodo.prototypeUp there.

In this way, Kodo becomes the factory method for generating objects, and the actual constructor is init.

Second arrow:However, we have to “cheat” the users to make them feel that:

Objects constructed with init are just like objects constructed with Kodo

How can this be done? The answer is the place indicated by the second arrow. I will assign Kodo.prototype to init.prototype In this way, all the methods defined on the Kodo prototype chain are automatically shared with the new objects (don’t forget that these objects are actually constructed with init).

To sum up:

  1. Kodo is an object factory method.

  2. Init is the actual construction method. In fact, it can be placed anywhere, just for convenience and distributed to Kodo’s prototype.

The answer from the mobile phone was rather crude. When you go home, use the computer to answer.