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 8889681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:19:13+00:00 2026-06-14T22:19:13+00:00

Someone on the RubyRogues podcast once said Learn CoffeeScript because CoffeeScript writes better javascript

  • 0

Someone on the RubyRogues podcast once said “Learn CoffeeScript because CoffeeScript writes better javascript than you do.” Sorry, can’t remember who said it…

So, I took a very simple WORKING javascript function:

togglingii.js

function pdtogglejs(id) {   $('div .partybackground').removeClass("hidden");  }

Which is being called by this line:

<a href="#" class="dctoggle" onclick="pdtogglejs('partybackground')">Read More...</a>

Then I converted it into this coffeescript:
toggling.js.coffee

pdtogglecs(id) ->
   jQuery('div .partybackground').removeClass("hidden")

and changed the html to reference the pdtoggle*c*s instead of pdtoggle*j*s.

I can see BOTH of them just fine in my application.js file:

(function() {

  pdtogglecs(id)(function() {
    return jQuery('div .partybackground').removeClass("hidden");
  });

}).call(this);
function pdtogglejs(id) {   $('div .partybackground').removeClass("hidden");  }
;
(function() {



}).call(this);

However, only the pure javascript works. The coffeescript always returns Uncaught ReferenceError: pdtogglecs is not defined.

Based on other stackoverflow questions it must be some sort of namespace error. Probably because of the way pdtogglecs is, itself, inside of a function?? However, I have tried defining the coffeescript function with: window.pdtogglecs, this.pdtogglecs, root.pdtogglecs and the coffescript one always fails with that error.

What am I missing??

Thanks!!

  • 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-14T22:19:14+00:00Added an answer on June 14, 2026 at 10:19 pm

    You have two problems, one is a bit of CoffeeScript syntax confusion and the other is the namespace problem that you know about.

    We’ll start by sorting out your syntax confusion. This:

    f(x) -> ...
    

    is interpreted like this:

    f(x)(-> ...)
    

    So when given this:

    pdtogglecs(id) ->
       jQuery('div .partybackground').removeClass("hidden")
    

    CoffeeScript thinks you’re trying to call pdtogglecs as a function with id as an argument. Then it thinks that pdtogglecs(id) returns a function and you want to call that function with your -> jQuery(...) function as an argument. So it ends up sort of like this:

    callback = -> jQuery(...)
    returned_function = pdtogglecs(id)
    returned_function(callback)
    

    And that’s nothing like your original JavaScript. You want to create a function named pdtogglecs which takes id as an argument and then runs your jQuery stuff:

    pdtogglecs = (id) ->
        # -----^ this is sort of important
        jQuery('div .partybackground').removeClass("hidden")
    

    You can see what’s going on by looking at the generated JavaScript.

    The namespace problem is easy and you can probably figure that out based on the other question you found. However, I’ll take care of it right here for completeness.

    CoffeeScript wraps each .coffee file in a self-executing function to avoid polluting the global namespace:

    (function() {
        // JavaScript version of your CoffeeScript goes here...
    })();
    

    That wrapper makes everything scoped to the .coffee file. If you want to pollute the global namespace then you have to say so:

    window.pdtogglecs = (id) -> ...
    

    You can also say:

    @pdtogglecs = (id) -> ...
    

    but I prefer the explicitness of directly referencing window, that also saves you from worrying about what @ (AKA this) is when you’re code is parsed.

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

Sidebar

Related Questions

Someone recently said that I can use Linq to SQL in my application even
(Someone edit the title if you can understand and define my problem better.) The
Someone said if the app in review status for more than 1 week, it
Someone can give me short explanation how to create bitmap runtime using GDI/GDI+ and
Someone recently taught me a useful thing: in css you can write the following:
Someone could recommend a better way to detect if a bitmap has Alpha channel
Someone recommend us to use HttpClient rather than HttpWebRequest in the code. I am
Someone enlightened me yesterday to the fact that Labels can have access keys. So
Someone must know this but can anyone help me with the following please When
Someone can tell me what API's to use to gather outlook contacts list ?

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.