3 ms·
function createQueue(){ var queue = []; return { enqueue: function(item){ queue.push(item); }, dequeue: function(){ re
by lollipop25 11y ago
function createQueue(){
var queue = [];
return {
enqueue: function(item){
queue.push(item);
},
dequeue: function(){
return queue.unshift();
}
};
}
var queue = createQueue();
Now you might think `queue` is inaccessible because it's in a closure, right? Wrong. Because I can do this:
var queue = createQueue();
var originalPush = Array.prototype.push;
Array.prototype.push = function(){
// turn all enqueued to foo
arguments[0] = 'foo';
originalPush.apply(this, arguments);
// Also know that "this" in here is the queue
}
You can easily monkeypatch things in JS. There are more creative and invasive ways to do this. You do have to know how the code runs in order to execute the right patches.