Jasper
Jasper

Reputation: 5238

Minimize object

I have a code like this:

var methods = {
    collapse: function(element) {
         modify(element);
    },
    other_method: function() {
         // ...
    }
};

function modify(element)
{
     console.log('collapse method');
}

Is it possible to minify collapse method to one line? So it should always call modify function.

Upvotes: 0

Views: 43

Answers (1)

Minko Gechev
Minko Gechev

Reputation: 25682

Try this:

var methods = {
    collapse: modify,
    other_method: function() {
         // ...
    }
};

function modify(element) {
     console.log('collapse method');
}

Because we have function declaration (not expression), modify is visible when you declare the object methods. The thing which is done here is just setting collapse to be equal to modify's reference.

This is the same as:

var modify = function (element) {
     console.log('collapse method');
}

var methods = {
    other_method: function() {
         // ...
    }
};
methods.collapse = modify;

Upvotes: 2

Related Questions