Skip to content

Instantly share code, notes, and snippets.

@sebinsua
Created January 21, 2014 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebinsua/8549923 to your computer and use it in GitHub Desktop.
Save sebinsua/8549923 to your computer and use it in GitHub Desktop.
Solution to scoping problem
// I cannot change this object.
var normalResponseObject = {
send: function (status, data) {
alert("This was executed.");
}
};
var generateProxiedResponseSender = function (res) {
var oldSend = res.send;
var context = this;
res.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.
context.status = arguments[0];
context.reaction = arguments[1];
oldSend.call(res, context.status, context.reaction);
};
};
var handleFinalOutputsToScreen = function () {
alert("did you run this?");
alert(JSON.stringify(this));
alert(this.reaction.hey);
alert(this.status);
};
var aScope = function () {
var context = {};
generateProxiedResponseSender.call(context, normalResponseObject);
setTimeout(handleFinalOutputsToScreen.bind(context), 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

This is my original solution to this problem: https://gist.github.com/sebinsua/8549914

It may not be beautiful, but it is at least becoming more explicit.

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