2

I have a 'custom' directory where I would like to store any changes to the prototypes of built-in objects. Each built-in object that's modified will have its own file (i.e custom/String.js for any modifications to String.prototype).

In addition to these files, I will have a file called custom/All.js which will be used to export the custom functionality to be used.

All.js

export * from './String'
export {Multiply} from './Array'

main.js

import * from './custom/All'

String.js

// something like this
export String.prototype.doSomething = function() {}

Can something like this be done?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
sookie
  • 2,437
  • 3
  • 29
  • 48
  • 1
    So you want to export it and modify the prototype? – Andrew Li Sep 26 '17 at 12:59
  • @AndrewLi Yes, pretty much. Essentially I would like to import the changes to String's prototype more-so than the function itself (if that's even possible) – sookie Sep 26 '17 at 13:04
  • @Rajesh No. That's even weirder to use. – Bergi Sep 26 '17 at 13:06
  • @Rajesh My preference is to modify `String.prototype` directly as this will be a widespread change across the codebase. Extending from String will require me to have to import this new class in any of the files that use it – sookie Sep 26 '17 at 13:06

1 Answers1

12

Of course it's still considered a bad idea to extend builtin prototypes, even in ES6, but if you insist on doing it anyway instead of a simple-to-use module of static helper functions:

You should not export anything. Those are mutations, and don't have any value. You only need to include the module code for its side effects.

// main.js
import './custom';

// custom/index.js
import './String';
import './Array';

// custom/String.js
String.prototype.doSomething = function() {};
Bergi
  • 630,263
  • 148
  • 957
  • 1,375