Home > Software design >  How to call two functions with same name?
How to call two functions with same name?

Time:08-20

I installed 2 NPM packages and they both have a random(); function how do I specify from which npm package am i calling the random method

here's my code:


import { random } from 'superheroes';
import { random } from 'supervillains';

console.log(random());
console.log(random());

CodePudding user response:

You can use import alias

import { random as randomSuperheroes } from 'superheroes';
import { random as randomSupervillains } from 'supervillains';

// You can call it using the alias name

console.log(randomSuperheroes()); // superheroes
console.log(randomSupervillains()); // supervillains

CodePudding user response:

You could supply an alias to (one of) the imports, like so:

import {random as alias} from 'supervillains';
  • Related