I'm actually studying Crockford's Javascript: the good parts. I am new to JavaScript so I'm having a difficult time to understand how this code works:
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
Here's what I think:
Being a method (a function inside an object), this
points to the Function
object, but why the need to return the object since i'm having access on it from inside the method? If i am right, this
is a reference, not a local copy, so:
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
};
Should work as well.
From the other side, in JavaScript a function without a return statement returns undefined
and assign it to Function.prototype.method
.
The Question
What's the point of returning this
?
Working example #1
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
var add = function(a, b) {
return a+b;
};
Function.method('add', add);
var f = function() {};
print(f.add(1,2));
Number.method('integer', function () {
return Math[this < 0 ? 'ceil' : 'floor'](this);
});
print((-10/3).integer());
Output:
-3 3
Working Example #2
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
};
var add = function(a, b) {
return a+b;
};
Function.method('add', add);
var f = function() {};
print(f.add(1,2));
Number.method('integer', function () {
return Math[this < 0 ? 'ceil' : 'floor'](this);
});
print((-10/3).integer());
Output:
-3 3
let me try to explain it. I didn't read that book, but an article Classical Inheritance in JavaScript of Douglas Crockford has one important sentence, related to that example about Function.prototype.method:
actually I am not familiar with that term, I think well known term is "Fluent Interface" or "Method Chaining", read that wiki page, there are examples in different languages, so you will understand it..
PS. @Gianluca Bargelli was a bit faster to provide example of using Function.prototype.method this way, so I don't post it in my answer
ADDON: how you can use it in terms of your example:
you see, integer() returns Number object, so you can call another method, instead of writing:
and few words about my way of using it, most of time I prefer to write "each method - new line with indent, for me this way is more readable:
this way I see where I start, and "new line/indent" tell me that I still modify that object. Compare it to long line:
or typical approach:
ADDON2: Usually I use this approach (Fluent interface pattern) when I work with entities e.g. Java code:
it allows me to construct
Person
object in easy way:Some people make it a bit different, they add new kind of methods to
set
/get
, and call itwith
:Now my previous example looks like:
This way we don't change meaning of set method (I mean we do not enhance it, so it work as most of developers expect... at least that people do not expect that
set
method can return anything ;) ). One interesting thing about these special methods - they can loose their self-documenting, but they improve readability when you use them (like in example withPerson
creation,withName
tells very well what exactly we are doing..to read more:
FluentInterface - Martin Fowler's description of that pattern
Fluent Interfaces in PHP
The Weekly Source Code 14 - Fluent Interface Edition - as for me short and good enough to see pros and cons (as well as links to other resources)