user419017
user419017

Reputation:

Coffeescript - call function by variable

I want to manage part of my layout with coffeescript. I have a left/right panel and I want to be able to switch between them. I have created something like this:

window.switchPanel = (panel = 'left', action = 'toggle') ->

  open = (panel) ->
    ...

  close = (panel) ->
    ...

  toggle = (panel) ->
    ...

My question is, how do I structure this so that I can call open/close/toggle by the action variable and can I use something so that I don't have to pass in the panel to every child function? Perhaps @panel?

Upvotes: 2

Views: 1914

Answers (2)

mu is too short
mu is too short

Reputation: 434665

I think you just want to throw your functions into an object so that you can access them by name:

window.switchPanel = (panel = 'left', action = 'toggle') ->
  funcs =
    open: (panel) ->
      ...
    close: (panel) ->
      ...
    toggle: (panel) ->
      ...

Then you can simply funcs[action](panel) inside switchPanel. If you don't want pass panel into the functions then you don't have to, they'll have access to panel simply by being defined within switchPanel:

window.switchPanel = (panel = 'left', action = 'toggle') ->
  funcs =
    open: ->
      ...
    close: ->
      ...
    toggle: ->
      ...

Then you'd just funcs[action]() and they could do what they like with panel.

Demo: http://jsfiddle.net/ambiguous/UV42x/

Some reading on JavaScript closures would clarify what's going on in the second version.

You might want to include an if(action !of funcs) check to make sure you don't try to use a bad action. Or, as Aaron Dufour notes in the comments, you could funcs[action]?() if you only need to check that action is valid once.

Upvotes: 4

JamesHoux
JamesHoux

Reputation: 3447

Few things:

1) I think you're creating obvuscated architecture by selectively calling methods within the object based on an input to the object. It begs the question "Why even create the methods? Why not just use a big IF ELSE IF type structure?" But others would disagree with me.

2) This is one of the confusing circumstances of CoffeeScript. You're dealing with something that would normally be clear if wrapped in explicit braces. I don't like javascript's crazy use of anonymous function wrappers everywhere, but I do believe in bracketed code. I defer to point 1. I think you're just making things more complicated by doing it this way.

3) Have you studied how coffeescript transforms the 'this' keyword? I'm pretty certain CoffeeScript does some kind of smart transformation on 'this' that deviates from standard javascript, which will mean nobody can answer this question for you except Coffee users. It won't be the same as javascript. Its relevant because in order to access 'panel' in the methods without passing it explicitly, you would need to use 'this.panel' or some other dereferencing mechanism to access the panel member from within one of the methods. I can't tell you how, but this information my help steer you in the right direction. The issue at hand is clearly a question of how to reference object members.

Upvotes: 0

Related Questions