Let me try to explain this. I have not read this book, but Douglas Crockford's classic JavaScript inheritance article has one important suggestion related to this example about Function.prototype.method:
He returns it. When I write a method that does not need to return a value, I usually return it. This allows for cascading programming style.
Actually, I am not familiar with this term, I think the well-known term is “ Free interface ” or “Method chain”, read this wiki page, there are examples in different languages, so you will understand this.
PS. @Gianluca Bargelli was a little faster to provide an example of using Function.prototype.method in this way, so I don't post it in my answer
ADDON: how can you use it in terms of your example:
Function.prototype.method = function (name, func) { this.prototype[name] = func; return this; } Number.method('integer', function () { // you add 'integer' method return Math[this < 0 ? 'ceil' : 'floor'](this); }) .method('square', function () { // you add 'square' method with help of chaining return this * this; }); console.info( (-10/3).integer().square() ); // <- again chaining in action
you see, integer () returns a Number object, so you can call another method instead of writing:
var a = (-10/3).integer(); console.info( a.square() );
and a few words about my method of use, most of the time I prefer to write "each method is a new line with indentation, for me this method is more readable:
Function.method('add', add) .method('sub', sub) .method('mul', mul) .method('div', div);
so I see where I start, and the “new line / indent” tells me that I am still modifying this object. Compare it with a long string:
Function.method('add', add).method('sub', sub).method('mul', mul).method('div', div);
or typical approach:
Function.method('add', add); Function.method('sub', sub); Function.method('mul', mul); Function.method('div', div);
ADDON2: I usually use this approach (Fluent interface pattern) when I work with objects, for example. Java Code:
public class Person { private String name; private int age; .. public String getName() { return this.name; } public Person setName( String newName ) { this.name = newName; return this; } public int getAge() { return this.age; } public Person setAge( int newAge ) { this.age = newAge; return this; } .. }
it allows me to easily build a Person object:
Person person = new Person().setName("Leo").setAge(20);
Some people do this a little differently, they add a new type of methods to set / get and call it with :
public class Person { private String name; private int age; .. public String getName() { return this.name; } public void setName( String newName ) { this.name = newName; } public Person withName( String newName ) { this.setName( newName );
Now my previous example looks like this:
Person person = new Person().withName("Leo").withAge(20);
Thus, we do not change the value of the set method (I mean that we do not improve it, so it works as expected by most developers), at least people do not expect the set method to return anything; )). The interesting thing about these special methods is that they can lose their self-documentation, but they improve readability when you use them (for example, in the example with Person creation, withName very well about what exactly we are doing.
To find out more:
FluentInterface - a description of this pattern by Martin Fowler
Free interfaces in PHP
The weekly source code 14 - Fluent Interface Edition - is short and good enough for me to see the pros and cons (as well as links to other resources)