Created
January 21, 2014 22:38
-
-
Save sebinsua/8549923 to your computer and use it in GitHub Desktop.
Solution to scoping problem
This file contains 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
// 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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.