Created
October 29, 2011 17:42
-
-
Save jacegu/1324844 to your computer and use it in GitHub Desktop.
Some weird stuff with contexts for a CoffeScript&Javascript beginner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| set_name = (name) -> @name = name | |
| set_name 'Juan' | |
| console.log name #Juan | |
| console.log @name #undefined | |
| #How context works here is pretty weird... | |
| #Looks like set_name is not evaluated on "this" context | |
| #This seems quite true if we do: | |
| set_name.call this, 'Juan' console.log @name #Juan | |
| #If we looking at the compiled Javascript: | |
| var set_name; | |
| set_name = function(name) { | |
| return this.name = name; | |
| }; | |
| set_name('Juan'); | |
| console.log(name); | |
| console.log(this.name); | |
| #I'd say that functions have no context by default. Am I right? | |
| #Or, maybe, the function itself is an object and has it's own context | |
| #Need some clarification on this one |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for your help @eamoderubio! It's crystal clear now :·)