Home > Enterprise >  How do I export a function from a module for a class that is built into javascript?
How do I export a function from a module for a class that is built into javascript?

Time:04-28

I'm trying to export a function to use in another module, but it's one that I'm adding to the String javascript class.

How would I export the function to use in another module?

This is what I tried and it didn't work

String.prototype.trimChars = function(chars) {...}
export {String}

How do I make it so that this function is added to the string class as trimChars?

CodePudding user response:

You can simply forgo the export, and import in another module. However, patching objects like this is not recommended, as it can introduce problems, if say, a browser adds a new function to String that happens to have the same name.

// in the other module
import "path/to/module.js";
  • Related