Export/Import ES6中内置对象的自定义函数?

Export/Import custom function of built-in object in ES6?

我有一个 'custom' 目录,我想在其中存储对内置对象原型的任何更改。每个被修改的内置对象都有自己的文件(即 custom/String.js 用于对 String.prototype 的任何修改)。

除了这些文件,我还有一个名为 custom/All.js 的文件,用于导出要使用的自定义功能。

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() {}

这样的事情能做吗?

当然it's still considered a bad idea to extend builtin prototypes, even in ES6,但如果你坚持这样做而不是简单易用的静态辅助函数模块:

你不应该export任何东西。那些是突变,没有任何价值。您只需要包含其副作用的模块代码。

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

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

// custom/String.js
String.prototype.doSomething = function() {};