在ES6中导出/导入内置对象的自定义功能?

时间:2017-09-26 12:56:56

标签: javascript ecmascript-6 es6-modules

我有一个'定制'目录,我想存储内置对象原型的任何更改。修改后的每个内置对象都有自己的文件(即custom/String.jsString.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() {}

可以这样做吗?

1 个答案:

答案 0 :(得分:7)

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

你不应该export任何事情。这些是突变,并没有任何价值。您只需要为其副作用包含模块代码。

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

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

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