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?