javascript - 在 ES6 中导出/导入内置对象的自定义函数?

标签 javascript ecmascript-6 es6-modules

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

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

All.js

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

ma​​in.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/

相关文章:

javascript - Bootstrap 导航栏子菜单事件状态不起作用

javascript - 如何在新的 es6 模块语法中表达此 Node 导出

javascript - 使用 es6-arrow 函数语法检查数组中是否有两项总和等于给定值

javascript - 不建议在 ES6 中使用 "use strict"?

javascript - Ajax -- 无法使数据作为 "x-www-form-urlencoded"发送

javascript - 使用 JavaScript/jquery 刷新文本字段中的值

javascript - 如何从数组集中返回索引 - 1

function - typescript :如何在没有TS2339错误的情况下将函数名称获取为字符串

javascript - 将 Babel 外部助手与 ES6 模块一起使用时出现问题 - Babel 重新排序导入/导出会破坏执行顺序

javascript - 对模块方法的内部引用与导出引用