Aleksandr Makov
Aleksandr Makov

Reputation: 2938

CoffeeScript calling a method of returned object ( chaining )

Say I've got the code:

cat = {
    feed: (food) ->
        alert "cat ate #{food}"
}

pets = {
    "maximus": cat
}

getPet = (name) ->
    pets[name]

How can I invoke the "feed" method of returned by "getPet" cat object? This is not a valid code:

getPet "maximus" feed "Fish"

In plain javascript it would look like this:

getPet("maximus").feed("Fish");

Upvotes: 2

Views: 432

Answers (1)

Sandro
Sandro

Reputation: 4771

You can't do chaining without the parentheses on the left-most parts of the chain.

getPet('maximus').feed 'fish'

Upvotes: 4

Related Questions