small medium large xlarge

Back to: All Forums  CoffeeScript
Jpenelopetelemachus000_pragsmall
17 Jun 2011, 23:22
Peter Aronoff (3 posts)

I’m a bit confused about the example on page 44 concerning @/this in a global context.

The book has this:

setName 'Lulu' 
console.log name	# 'Lulu' 
console.log @name	# undefined

But I can’t reproduce that (node 0.4.8, coffeescript 1.1.1)

coffee> setName = (name) -> @name = name
[Function]
coffee> setName 'Lulu'
'Lulu'
coffee> console.log name
ReferenceError: name is not defined
    at repl:2:1
    at Object.eval (/Users/telemachus/local/node-v0.4.8/lib/node_modules/coffee-script/lib/coffee-script.js:89:15)
    at Interface.<anonymous> (/Users/telemachus/local/node-v0.4.8/lib/node_modules/coffee-script/lib/repl.js:39:28)
    at Interface.emit (events.js:64:17)
    at Interface._onLine (readline.js:153:10)
    at Interface._line (readline.js:408:8)
    at Interface._ttyWrite (readline.js:585:14)
    at ReadStream.<anonymous> (readline.js:73:12)
    at ReadStream.emit (events.js:81:20)
    at ReadStream._emitKey (tty_posix.js:307:10)

coffee> console.log(@name)
undefined

Edit apologies - I see that the problem appears only in coffee’s repl. If I run the code from a file, all is fine. Presumably the repl lacks a global object (or doesn’t do any assignments in a global context?) Still it might be worth mentioning or clarifying for people who run some of the code in the repl as they read (which I’ve been doing).

Trevor_burnham__goofy_headshot__pragsmall
18 Jun 2011, 00:33
Trevor Burnham (54 posts)

Wow, I think you actually just stumbled onto a weird bug in the CoffeeScript REPL. Thanks! I’ve reported the issue here: https://github.com/jashkenas/coffee-script/issues/1444

Jpenelopetelemachus000_pragsmall
18 Jun 2011, 12:59
Peter Aronoff (3 posts)

Thanks for the quick response. Unfortunately, I think I’m now more confused than before.

When using the exact code from the book, I get three sets of different results, depending on how I do things. (I’m going to copy my setName.coffee and setName.js files below, but they should be identical to the ones you provide.)

If I use the Cofeescript REPL, it blows up. This is a bug, so ok.

# If I use the Node REPL, I get ‘Lulu’ for both @console.log(name)@ and @console.log(this.name)@. # If I run the coffee file through the @coffee@ interpreter or the js file that the coffeescript compiles down to through the @node@ interpreter, then I get ‘Lulu’, ‘undefined’ (as you say in the book).

Thinking this over, I no longer understand any of it. :) Why should it ever produce ‘Lulu’, ‘undefined’ rather than ‘Lulu’, ‘Lulu’? If I’m in the global context, isn’t @name@ just an implicit way of saying @this.name@? Second, why does the Node REPL also produce different results than running the script on the command line via the Node interpreter? I apologize if I’m being dense, but this has me boggled.

Anyhow, here’s the exact code I’m working with, so you can check my results. It’s your ‘setName.coffee’ file, but I removed everything except the global example.

setName.coffee:

setName = (name) -> @name = name
setName 'Lulu'
console.log name     # 'Lulu'
console.log @name    # undefined

setName.js:

(function() {
  var setName;
  setName = function(name) {
    return this.name = name;
  };
  setName('Lulu');
  console.log(name);
  console.log(this.name);
}).call(this);

Edit: One more data point: v8 gives ‘Lulu’, ‘Lulu’ both in the REPL and if I run the script through the interpreter.

Trevor_burnham__goofy_headshot__pragsmall
18 Jun 2011, 15:31
Trevor Burnham (54 posts)

Thanks for continuing to test this. I posted an answer on Stack Overflow: http://stackoverflow.com/questions/6396467/a-puzzle-about-this-in-javascript-coffeescript/6397029#6397029

You must be logged in to comment