Skip to content

Instantly share code, notes, and snippets.

@sebinsua
Created January 21, 2014 22:37
Show Gist options
  • Save sebinsua/8549914 to your computer and use it in GitHub Desktop.
Save sebinsua/8549914 to your computer and use it in GitHub Desktop.
Problem with scoping
// I cannot change this object.
var normalResponseObject = {
send: function (status, data) {
alert("This was executed.");
}
};
var aScope = function () {
var reaction, status;
var oldSend = normalResponseObject.send;
normalResponseObject.send = function () {
// Problem 2: I want to be able to move the code which generates an object
// which wraps normalResponceObject elsewhere, but yet still
// have it set a reaction and status which can be accessed by another function.
status = arguments[0];
reaction = arguments[1];
oldSend(status, reaction);
};
setTimeout(function () {
// Problem 1: assume this is a function which was executed on a particular event happening.
// I want to find way of explicitly passing in reaction and status, instead
// of depending on closures.
alert(reaction.hey);
alert(status);
}, 500);
};
var anotherScope = function () {
// Assume that this was being called later on against the normalResponseObject but not here.
normalResponseObject.send(200, { hey: 5 });
};
aScope();
anotherScope();
@sebinsua
Copy link
Author

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