Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6: Re-defining exported function

Given a 3rd party library that exports a function and uses this function in its internal logic - is there any way to re-define this function? For example:

third-party.js

export function a() {
 console.log('a');
}

export function b() {
 a();
}

my-module.js

import * as tp from 'third-party';

//Re-define, something like this
Object.defineProperty(tp, 'a', { writable: true, value: () => console.log('c')});

//Call b and get the re-define function called
tp.b(); //Expected output: 'c'

A few highlights:

  • I don't need it for testing, but for production (yes, I know it would be a dirty hack)
  • Yes, I'm aware of imports being live read-only views, I'm looking for a workaround to overcome this constraint
  • No, I can't change the code of the 3rd party library
  • I need my change to actually change the logic of the 3rd party. I'd like to call a function that calls a and not a itself.
like image 678
JeB Avatar asked Mar 16 '19 16:03

JeB


People also ask

What is difference between export and export default?

Exports without a default tag are Named exports. Exports with the default tag are Default exports. Using one over the other can have effects on your code readability, file structure, and component organization. Named and Default exports are not React-centric ideas.

How do I export multiple functions?

To export multiple functions in JavaScript, use the export statement and export the functions as an object. Alternatively, you can use the export statement in front of the function definitions. This exports the function in question automatically and you do not need to use the export statement separately.

How do you use export functions?

Use named exports to export a function in JavaScript, e.g. export function sum() {} . The exported function can be imported by using a named import as import {sum} from './another-file. js' . You can use as many named exports as necessary in a file.

How do you export default multiple functions React?

Use named exports to export multiple functions in React, e.g. export function A() {} and export function B() {} . The exported functions can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a single file.


1 Answers

Exported module is read-only. So, you can't do such.

delete tp.a;
tp.a = () => {
  console.log('c')
}
tp.a() // 'c'
tp.b() // You'll still get 'a'
// it's because, b is calling exported function a

If you wish tp.b() need the value overridden, then you don't export them but call in an instance. In your example code, just export a not b. But since, you're trying to override it from the third-party library. It's not possible to do so.

But if you insist using them, then you must override both functions.

const obj = {...tp}
obj.a = () => {
  console.log('c')
}

obj.b() // 'a'

obj.b = () => {
  obj.a()
}

obj.b() // 'c'
like image 185
Bhojendra Rauniyar Avatar answered Oct 07 '22 12:10

Bhojendra Rauniyar