Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8257319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:09:43+00:00 2026-06-08T02:09:43+00:00

I can’t fugure out how to pass an anonymous function that takes a parameter.

  • 0

I can’t fugure out how to pass an anonymous function that takes a parameter. Here is my situation:

class MyClass
  constructor: (@a) ->
    console.log("MyClass: created " + @a)

  show: ->
    console.log("MyClass: show " + @a)

then, with UnderscoreJS, this works:

_.map listOfMyClassObjects, (obj) -> obj.show

but I want to wrap the call to map in a separate function for convinience:

allMyClass(fun) ->
  _.map listOfMyClassObjects, fun(obj)

so that later I can do:

allMyClass((obj) -> obj.show())

but the browser console says:

Uncaught ReferenceError: fun is not defined
  (anonymous function)
  require../browser.CoffeeScript.run
  ...

What is the correct synthax? Also, is it possible to simplify like this?

 allMyClass(fun) ->
    _.map listOfMyClassObjects, obj[fun]()

 allMyClass(show())

UPDATE:

As per Thilo’s answer, there was the syntax mistake in the function call. But also, there was a mistake in calling the function on the map iteration result. The working version is this:

allMyClass = (fun) ->
  _.map listOfMyClassObjects, (obj) -> fun(obj)

Still wandering if there is a shorter version of passing the class method to the allMyClass function though.

UPDATE2:

Simplification is possible like this:

allMyClass = (fun) ->
  _.map listOfMyclassObjects, (obj) -> obj[fun]()

allMyClass("show")

Passing arguments to the fun would require passing more arguments all in all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:09:44+00:00Added an answer on June 8, 2026 at 2:09 am

    Did you mean to define a function

    allMyClass = (fun) ->
       _.map listOfMyClassObjects, fun(obj)
    

    or method

    allMyClass : (fun) ->
       _.map listOfMyClassObjects, fun(obj)
    

    Without the = or : you were just calling allMyClass.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can we change the default action of the edit selected row button? Here is
Can I be sure about the order in a Python dictionary? The function op.GetTangent(id)
Can anybody shed some light on getDay() in Javascript please. Here datepicker is textbox
Can I register a .Net COM class with the SingleUse -flag? The reason I
Can't figure out how to do this in a pretty way : I have
Can we change the color of the divider? Apple documentations says, that we can
Can someone please explain why this doesn't work? MyClass myClass1 = new MyClass(); object
Can't seem to figure out what's wrong with the simple getJSON call below. It's
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Can't work out a way to make an array of buttons in android. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.