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?

Lee Quarella
  • 4,662
  • 5
  • 43
  • 68
  • 5
    Must resist urge to make mom joke.... – Jesus Ramos Jul 20 '11 at 01:37
  • 1
    Sigh. Duplicate of http://stackoverflow.com/questions/6089992/cant-find-variable-error-with-rails-3-1-and-coffeescript. See also http://stackoverflow.com/questions/6099342/how-can-i-use-option-bare-in-rails-3-1-for-coffeescript. – Trevor Burnham Jul 20 '11 at 13:13
  • Sorry Trev, missed those. What is best practice after realizing your question is a dup, should I just delete it? – Lee Quarella Jul 20 '11 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. – jcollum Jan 07 '13 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. – jcollum Jan 07 '13 at 22:16

4 Answers4

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

  • 1
    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? – Propeller Mar 22 '12 at 18:50
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
Jamie Wong
  • 18,104
  • 8
  • 63
  • 81
  • 1
    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) – Lee Quarella Jul 20 '11 at 01:39
  • 1
    You can - see the two options I stated. You just have to make sure you export it to the global scope. – Jamie Wong Jul 20 '11 at 01:40
  • Ah, that's a little annoying. I guess console debugging is not used by many people? – Lee Quarella Jul 20 '11 at 01:41
  • 2
    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. – Jamie Wong Jul 20 '11 at 01:42
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.

liammclennan
  • 5,295
  • 3
  • 34
  • 30
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

Community
  • 1
  • 1
sajesh Nambiar
  • 689
  • 2
  • 10
  • 25