Re:The Problem with Implicit Scoping in CoffeeScript
by showell30 on Thursday December 22, 2011 @03:16PM
(#3383072)
When I said the two programs have the same behavior, I was simply referring to the end result--what they output. I understand how both languages work here. Ruby solves a problem that simply doesn't exist in CS. In JS/CS "bar" never implicitly refers to "this.bar", so there's no ambiguity in the first place.
Re:The Problem with Implicit Scoping in CoffeeScript
by showell30 on Thursday December 22, 2011 @03:25PM
(#3383101)
Your example does demonstrate how CS works. When you assign bar a new value inside its original scope, the value of bar does indeed change.
bar = ->
"I called bar!"
foo = ->
console.log bar()
bar = "I manually assigned bar!"
return bar
console.log foo()
console.log bar()
Here is a slightly less contrived example: log = (data) ->
console.log data
enhance_logging = ->
i = 0
log = (data) ->
i += 1
console.log i, data
log "no line numbers"
enhance_logging()
log "line one"
log "line two"
Here is the output: > coffee foo.coffee
no line numbers
1 'line one'
2 'line two'
Here's a better example:
Ruby:
Coffeescript: