Skip to content

Instantly share code, notes, and snippets.

@5v3n
Created November 6, 2010 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5v3n/665319 to your computer and use it in GitHub Desktop.
Save 5v3n/665319 to your computer and use it in GitHub Desktop.
sys = require 'sys'
#oo love:
class MyMath
square: (x) ->
x * x
cube: (x) ->
this.square(x) * x
#functional love:
printSquare = (number_to_process) ->
"square(#{number_to_process}) = #{myMath.square(number_to_process)}" #<- why does this work? myMath should be out of scope in here!
#let's get something rolling:
myMath = new MyMath
x = 3
#mind the nice string processing:
sys.puts printSquare(x)
sys.puts "cube(#{x}) = #{myMath.cube(x)}"
#what's next - dynamic dispatching ;-)?
@5v3n
Copy link
Author

5v3n commented Nov 6, 2010

Having heard a lot about async node.js coolness lately, but seeing JavaScript as a bit... not that elegant, I was glad to find CoffeeScript. I'm totally stoked by CoffeeScript right now :-). But what's happening in line 10 seems a bit strange to me. Is that strange scoping a JavaScript "feature", or a CoffeeScript bug?

@jashkenas
Copy link

This is how JavaScript works, I'm afraid -- all variable declarations are hoisted to the top of the current scope. printSquare closes over myMath, and before printSquare runs, it's given a value. Try this code, for a clearer example of the behavior:

(function(){
  var f = function(){ alert(a); };
  var a = 100;
  f();
})();

@5v3n
Copy link
Author

5v3n commented Nov 6, 2010

Thanks for the answer - I was already affraid to hear something like that ;-). So it's basically a kind of global variable - and it's initialization takes place before all the over code, right?

P.S.: Just saw that you're "The CoffeScript Guy" ;-). Awesome work so far! Being a Java dev playing with Ruby, hearing of all the great features node.js offers - CoffeScript could be the bridge between my taste of coding and these magic async features server side javascript.

I like the mix of ruby style - and feel somehow reminded of maple / mathematica function when I see square = (x) ->
x * x
.

Bottom line: I'm stoked! ;-)

@jashkenas
Copy link

Not quite -- it's not a global variable, it's just that all variable declarations effectively take place at the top of the function. For example, the following two are the same:

var f = function() {
  var a;
  a = 10;
  call(a);
};

var f = function() {
  a = 10;
  call(a);
  var a;
};

@5v3n
Copy link
Author

5v3n commented Nov 6, 2010

Alright, I see - the declaration & initialization are moved on top of the rest.

But how can the variable be accessed inside the function, if it's not global? I would expect the function to have it's own scope...

I'm a total noob concerning js, sorry for the potentially stupid question ;-).

@jashkenas
Copy link

@5v3n
Copy link
Author

5v3n commented Nov 6, 2010

OK, dynamic scoping... if I remember that right, there's a global binding for variables in that. Reminds me of Pascal back then, now I understand.

Wonder if that's an aspect that Crockford discusses in "JS: The good Parts" ;-).

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment