Created
January 21, 2014 22:37
-
-
Save sebinsua/8549914 to your computer and use it in GitHub Desktop.
Problem with scoping
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 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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See: https://gist.github.com/sebinsua/8549923