Skip to content

Instantly share code, notes, and snippets.

@freshtonic
Created January 22, 2012 08:01
Show Gist options
  • Save freshtonic/1656210 to your computer and use it in GitHub Desktop.
Save freshtonic/1656210 to your computer and use it in GitHub Desktop.
Weird JS problem
(function() {
var fixedAssetColumns, totalsRowTemplate;
fixedAssetColumns = [
{
name: 'description'
}, {
name: 'date'
}
];
console.log(fixedAssetColumns); // Prints fixedAssetColumns to console (as one would expect)
totalsRowTemplate = function() {
return tr(function() {
// Uncaught ReferenceError: fixedAssetColumns is not defined
// Weird because this is ultimately nested within the same function scope and should be captured.
return _.each(fixedAssetColumns, function(col) {
return td;
});
});
};
}).call(this);
@freshtonic
Copy link
Author

Why does is an Uncaught ReferenceError thrown when fixedAssetColumns is referenced in the totalsRowTemplate function? It should be bound to the variable defined in the surrounding function scope.

@scottharvey
Copy link

You haven't defined 'td' anywhere, not sure if that's the problem you are hitting but it would definitely cause a problem somewhere.

@freshtonic
Copy link
Author

Forget the td, I trimmed things down to make a smaller example and left in some stuff by accident. I'll edit it out.

@freshtonic
Copy link
Author

Was nothing to do with my JS. It was CoffeeKup converting a function back to a string, and reevaluating in different context thus losing the closed-over variables. Thanks for taking the time to look though :)

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