1

I want to slightly modify the code of some function, exposed to me by a third party module. I was thinking of copying the function and modifying it, but the problem is that it relies on other functions and variables that exist in its lexical scope. I do not have a problem with the infamous "this" binding, but with the scope itself.

For example, this is the third party library:

var someGlobalVar;

function someInnerFunction(){
    //Does something...
}

function functionIWantToAlter(){
    //I want to copy the implementation of this function, and alter parts of it,
    // but i need to preserve its lexical scope.
    const x=5;
    const y=10;

    console.log(x+y);


    someInnerFunction(someGlobalVar)//The function calls some inner function, and uses an inner variable
}

module.exports = functionIWantToAlter;

Then, in my code, i want to literally copy this function, but to preserve its lexical scope:

const functionIWantToAlter = require('third-party-module');
//Maybe here it's somehow possible to "bind" my override function, to the lexical
//scope of "functionIWantToAlter"?

function override(){
    //Do here something different, than the original function does

    const x=1;
    const y=2;

    console.log(y*x);

    someInnerFunction(someGlobalVar)//Some of the implementation is the same,
    // but my code doesn't have access to the original lexical scope.
}

Is there a way this could be done?

Edit: The library i need to "patch" is node-fetch. This is the function: https://github.com/node-fetch/node-fetch/blob/master/src/index.js

In the npm_module itself the code comes in one big JS file, but i don't think it makes any difference. Any idea how to do it?

i.brod
  • 3,993
  • 11
  • 38
  • 74
  • This is not vanilla JS. Please tag the question with any frameworks you are using. – Wais Kamal Jun 07 '20 at 10:00
  • Per e.g. https://stackoverflow.com/questions/4472529/accessing-variables-trapped-by-closure you can't access the variables in the closure from outside. – jonrsharpe Jun 07 '20 at 10:04
  • 3
    @WaisKamal I don't see any framework usage here. – jonrsharpe Jun 07 '20 at 10:04
  • The `module` object is not defined in ordinary JS. – Wais Kamal Jun 07 '20 at 10:07
  • 3
    `module.exports` is pretty standard `node.js` – Dane Brouwer Jun 07 '20 at 10:08
  • 1
    @DaneBrouwer that's true. Also, it doesn't really alter anything whether it's `module.exports` or `export` or the older module pattern the result would be the same. – VLAZ Jun 07 '20 at 10:10
  • 1
    Need to patch it *to do what*? The point of node-fetch is to be consistent with the browser's fetch, if that's not a goal for you you could look at alternatives that provide for e.g. interceptors. – jonrsharpe Jun 07 '20 at 10:45

2 Answers2

2

If you had a reference to the original scope you could do

 with(originalScope) {
  function fake() {
    //...
  }
 }

but as you don't have any way to access the original scope, this is impossible. Also this is generally a terrible idea.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
1

Scrap that idea. Fork the library and make your edits.

Alternatively you can npm “patch” the library.

Ibraheem
  • 2,168
  • 20
  • 27
  • By "npm patching" you mean creating my own copy of the library? If so, is it even legal? – i.brod Jun 07 '20 at 11:13
  • 1
    @i.brod depends on the license of the library. This one is [under MIT license](https://github.com/node-fetch/node-fetch/#license), so you have complete freedom to do what you want with it. – VLAZ Jun 08 '20 at 14:05