16

Playing a little with coffeescript and Rails 3.1.0.rc4. Have this code:

yourMom = (location) ->
 console.log location
yourMom "wuz hur"

When the page loads, this outputs "wuz hur" properly. But when I try to call

yourMom("wuz hur")

from the chrome js console (as I do sometimes to test normal JS functions), I get a "ReferenceError: yourMom is not defined"

Are functions generated by coffeescript available in this way?

asked Jul 20, 2011 at 1:32
5
  • 5
    Must resist urge to make mom joke.... Commented Jul 20, 2011 at 1:37
  • 1
    Sigh. Duplicate of stackoverflow.com/questions/6089992/…. See also stackoverflow.com/questions/6099342/…. Commented Jul 20, 2011 at 13:13
  • Sorry Trev, missed those. What is best practice after realizing your question is a dup, should I just delete it? Commented Jul 20, 2011 at 17:54
  • 4
    @TrevorBurnham "Sigh.": this is a bit obnoxious. Please don't be obnoxious to new people. It's verging on the RTFM attitude from the C++ mailing list days. Commented Jan 7, 2013 at 22:15
  • @LeeQuarella I'd just put "Duplicate of XYZ (Link)" at the top of the post. These days, dupes will get marked as such (with a link to the dupe) and closed pretty quickly. Commented Jan 7, 2013 at 22:16

4 Answers 4

39

an easier way to share global methods/variables is to use @ which means this.

@yourMom = (location) ->
 console.log location
yourMom "wuz hur"

Nicer syntax and easier to read, but I don't encourage you to create global methods/variables

answered Sep 13, 2011 at 9:48
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this. :) Although this may be an old question, may I ask why you don't encourage creating global methods / variables? If you don't encourage that, then hw are people supposed to call the functions then from their web pages?
13

This happens because coffeescript wraps everything in a closure. The JavaScript output of that code is actually:

(function() {
 var yourMom;
 yourMom = function(location) {
 return console.log(location);
 };
 yourMom("wuz hur");
}).call(this);

If you want to export it to the global scope, you can either do:

window.yourMom = yourMom = (location) ->
 console.log location

or

this.yourMom = yourMom = (location) ->
 console.log location
answered Jul 20, 2011 at 1:37

4 Comments

SOoooo, I can't call yourMom from the console? Is there a way to make the function available there? Console debugging has been very helpful for me, kinda hesitant about using coffeescript without that ability... (this post was made before answer editted)
You can - see the two options I stated. You just have to make sure you export it to the global scope.
Ah, that's a little annoying. I guess console debugging is not used by many people?
It is - but you should be specifically trying to avoid publishing many many global variables in JavaScript. Throw them into the global scope for when you want to debug them, then remove them from the global scope once you're done messing around with them.
2

I'm not sure about Rails but the CoffeeScript compiler has an option (--bare) to compile without the function wrapper. Fine for playing but it does pollute the global scope.

answered Jul 20, 2011 at 5:20

Comments

0

this link might solve your problem Rails - Calling CoffeeScript from JavaScript Wrap your functions in a unique namespace and then you can acess these functions from wnywhere

answered Oct 3, 2012 at 5:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.