Functions with public properties

Posted on

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:


$'url', data);

    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) {

$http.get = function(url) {
    return $http({method : 'get', url: url});

$ = function(url, data) {
    return $http({method : 'post', url: url, data: data});


Leave a Reply

Your email address will not be published. Required fields are marked *