Skip to content

Instantly share code, notes, and snippets.

@jacegu
Created October 29, 2011 17:42
Show Gist options
  • Select an option

  • Save jacegu/1324844 to your computer and use it in GitHub Desktop.

Select an option

Save jacegu/1324844 to your computer and use it in GitHub Desktop.
Some weird stuff with contexts for a CoffeScript&Javascript beginner
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
@jacegu
Copy link
Author

jacegu commented Oct 31, 2011

Thanks for your help @eamoderubio! It's crystal clear now :·)

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