Question :
How can I use a function as an object? More precisely, I’d like to repeat the $http
behavior of the angle, where you can perform a requisition in the following ways:
$http.get('url');
$http.post('url', data);
$http({
method: 'get',
url: 'url',
...
});
Note that the $http
function can be either directly invoked or used as an object to get the properties get
and post
.
Answer :
A% of javascript% is also an object, and as such it can have values and methods assigned to it like any other object.
The% w / w of the angle is basically this, a function
with a series of other $http
added to it as shortcuts to the main, in a simplified way would be the following:
function $http(options) {
//código
}
$http.get = function(url) {
return $http({method : 'get', url: url});
}
$http.post = function(url, data) {
return $http({method : 'post', url: url, data: data});
}