If you're trying to define a module with a similar API...
Function
s in JavaScript are a type of Object
, so references to them can be passed around and they can be given properties, including other function
s as methods.
In this case, request
is just a function
with get
assigned as one of its properties:
function request(method, url) {
// ...
return new Request(method, url);
}
request.get = function(url, data, fn){
var req = request('GET', url);
// ...
return req;
};
With either, the value return
ed is a Request
instance, which has an end
method that continues to return
the instance.
Request.prototype.end = function(fn) {
// ...
return this;
};
This allows for a fluent API with method chaining:
request
.get('...', function () {})
.end(function () {});
.end(function () {});
// that does the same as...
var req = request.get('...', function () {});
req.end(function () {});
req.end(function () {});