我有一个“自定义”目录,我想在其中存储对内置对象原型(prototype)的任何更改。每个被修改的内置对象都有自己的文件(即 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 , 但如果你坚持这样做而不是简单易用的静态辅助函数模块:
你不应该导出
任何东西。那些是突变,没有任何值(value)。您只需要包含其副作用的模块代码。
// main.js
import './custom';
// custom/index.js
import './String';
import './Array';
// custom/String.js
String.prototype.doSomething = function() {};
关于javascript - 在 ES6 中导出/导入内置对象的自定义函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46427232/