0

Ok, I wouldn't think to do this in C#, but javascript is designed with much more flexibility in access.

there's a plugin like this

(function($)
{
    ...more stuff
    var results={a:1,b:2}  //this I need to modify
    var someData={x:1}
    send = function(){
        //send results ajax
    };

    if(typeof beforeSend=='function')
        beforeSend(someData) //hook to use results
})(jQuery)

So, in my own code, I have the function window.beforeSend = function(d){}

and it does have the someData which is in the scope I need to modify. But here's the question: How can I modify the results var that's within the closure before it sends it. I need to add

window.beforeSend = function(d){
    window.quantumTunnelThroughScope.results['c']=1
}

The reason I need to do this is because I cannot modify the code of the plugin. Of course if I add the beforeSend within the closure, it works, but then I'm modifying the library which I'm not allowed to do in this case.

I've seen some awesome eval('this.xx' =function ) etc etc but I can't make it work.

EDIT: I clarified that actually it's a different var in the same scope that needs to be edited

4
  • @T.J.Crowder well because I need to add c=1 to results before it sends them Aug 18, 2016 at 17:04
  • @T.J.Crowder yes, I apologize, I reduced the code down to it's essence but messed up by passing the wrong variable in the example Aug 18, 2016 at 17:10
  • is beforeSend(someData) a function which is originally present in the plugin? Aug 18, 2016 at 17:46
  • @JeroenVervaeke yes, just as it shows there. it's already there, and while it doesn't provide access to the scope, it does run "before" the results are sent so it just happens to be a good place to insert the code to modify the results var Aug 18, 2016 at 23:29

1 Answer 1

1

No, there's no reasonable way for beforeSend to reach into that closure and modify results. results in the code presented is entirely private to code within that closure.

The unreasonable way to try to do it is to decompile and recompile the plugin function, via eval, and insert a call to a function before the beforeSend that lets us modify results:

(function($) {
  $.run = function() {
    // You mentioned "ajax," so let's make this
    // asynchronous
    setTimeout(function() {
      var results = {
        a: 1,
        b: 2
      };
      var someData = { // Need to modify this
        x: 1
      };
      send = function() {
        //send results ajax
      };

      if (typeof beforeSend == 'function') {
        beforeSend(someData); //hook to use results
      }

      console.log("in plugin, results = ", results);
    }, 10);
  };
})(jQuery)

window.modifyResults = function(d) {
  return ["new", "results"];
};
window.beforeSend = function(r) {
  r.c = 1;
};

jQuery.run = (function() {
  // Function#toString, on nearly all browsers, returns the source
  // code of he function (or something near to it) except on functions
  // implemented in native code. We take that string and replace
  // the "beforeSend(someData);" call with two calls, the first of
  // which lets us modify the `results` variable. Then we use eval
  // to turn that back into a function, and assign the result to
  // where the plugin put its function originally.
  return eval("(" + jQuery.run.toString().replace(
    "beforeSend(someData);",
    "results = modifyResults(results); beforeSend(someData);"
  ) + ")");
})();

jQuery.run();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

But may or may not work, depending on how the plugin is written, as it lifts it out of its original scope and recompiles it in the scope of our function updating jQuery.run.

I think I'd prefer to take the hit of modifying the plugin. :-)


Note: In the above, I've used a "static" jQuery function. If the plugin you're replacing provides an instance function, the kind you can call on jQuery instances, e.g. the bar in $(".foo").bar(), you'll find it on jQuery.fn instead of jQuery:

jQuery.fn.pluginFunction = eval(...);
5
  • I'm apologize it's not clear, but by "before" I'm just referring to the beforeSend hook that gets run before the actual send function. That beforeSend hook is actually in the code of the closure and gets called already before the send, so in my own code, if I put the magic scope modifying code in that hook function, it WILL run before the send Aug 18, 2016 at 17:13
  • Also, I feel like since I can see the object if I browse the jquery object, I should be able to access it somehow in code. Aug 18, 2016 at 17:14
  • Also, if I could inject my function into the scope itself, then of course it would have access to the variable Aug 18, 2016 at 17:15
  • @AwokeKnowing: I've added an unreasonable way that may work. :-) Aug 18, 2016 at 17:19
  • 1
    Thanks, that's what I'm looking for. I appreciate your evaluation of which is better, and that's exactly what I wanted to be able to evaluate before deciding the course of action. I'll see if this works in this instance. I also wanted to bring my javascript power to the next level for those extreme cases and my general understanding. Aug 18, 2016 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.