small medium large xlarge

Back to: All Forums  CoffeeScript
10 Dec 2015, 22:59
Kadu Diógenes (9 posts)

Exercise 2: It’s not mentioned in what context the func must be executed. I used @, null and undefined and for the three cases it worked and if I console.log the value of @ in the func I get the following:

{ global: [Circular],
   process {
     title: 'node',
# tons of other things

I guess that the context is the global scope. Use null or undefined is a valid way to call a function in global scope context?

Exercise 4: I don’t know if I find a valid reason, since the only thing I could think about is that this is a way to differ between arguments and expressions. Is it right?

Exercise 7: I though that it would print ‘It works!’ because I interpret this part wrong: “There is one more detail worth mentioning—you can use arbitrary expressions as default arguments, though this generally isn’t recommended. If you do so, the expression will be executed from whatever context the function is being called in, before any expression in the function body and only if the assignment is made. In other words, the following two expressions are exactly equivalent.” from the “Default Arguments (arg=)” subchapter.

I understand that the expression to assign the default argument would be evaluated in the context of the caller, but after looking at the generated javascript this appear to not work due x argument shadowing global x, right?

x = true;
showAnswer = function(x) {
  if (x == null) {
    x = x;
  return console.log(x ? 'It works!' : 'Nope.');