The code is taken from the book http://habrahabr.ru/post/241587/ . As far as I understood, in JS there is no interface concept, as in Java. But the author writes a commentary on the code:

The program will communicate with the objects of the cells through a well-defined interface. Cell types will not be hard coded. We will be able to add new cell styles - for example, underlined cells in the header. And if they support our interface, they will just work, without any changes in the program. Interface:

minHeight () returns a number indicating the minimum height that the cell requires (expressed in lines)

minWidth () returns a number indicating the minimum width the cell requires (expressed in characters)

draw (width, height) returns an array of length height, containing sets of strings, each of which is width wide. This is the contents of the cell.

Next code:

function rowHeights(rows) { return rows.map(function(row) { return row.reduce(function(max, cell) { return Math.max(max, cell.minHeight()); }, 0); }); } function colWidths(rows) { return rows[0].map(function(_, i) { return rows.reduce(function(max, row) { return Math.max(max, row[i].minWidth()); }, 0); }); } 

I understand the role that max and cell play, how map and reduce work. But I do not understand what in this case does .minHeight (), minWidth (), draw ()? Is this an interface or is it a method? If this is just a method, then where is the code that runs when it is called?

In the following piece of code, these methods are encountered again:

 function UnderlinedCell(inner) { this.inner = inner; }; UnderlinedCell.prototype.minWidth = function() { return this.inner.minWidth(); }; UnderlinedCell.prototype.minHeight = function() { return this.inner.minHeight() + 1; }; UnderlinedCell.prototype.draw = function(width, height) { return this.inner.draw(width, height - 1) .concat([repeat("-", width)]); }; 

Those. UnderlinedCell.prototype.minWidth is the description of this method, and return this.inner.minWidth(); - is it a recursion?

    2 answers 2

    Yes, it is implied that UnderlinedCell.prototype.minWidth will determine the interface of the object. Also (unlike classical interfaces), these interfaces contain an implementation, which in this case simply throws a call to an inner object (which was passed to the constructor), but there may be other interface implementations.

    The UnderlinedCell object guarantees that it has methods for counting the width and height of the cell and will also be able to draw it. But the concrete implementation of these algorithms is already on the inner object. This means that the UnderlinedCell object must make sure that the object that implements it has the required methods.

    Thus, an Object-Interface is obtained that guarantees the declared behavior and does not contain algorithms that implement this behavior. And the inner object implements this interface.

    Another implementation of interfaces in javascript is possible:

    http://javascript.ru/forum/misc/14657-interfejjsy-v-javascript.html http://jscriptpatterns.blogspot.ru/2013/01/javascript-interfaces.html

      There are no interfaces in JavaScript, but you can invent them if you wish. In general, “interface” means “contract” or “contract”. There are languages ​​where such contracts are explicitly present and checked for execution by the compiler. There are no such constructions in JavaScript. Therefore, in your context, it is just an agreement about what methods will be and what they should do.