2017-09-26 90 views
1

我有一個'自定義'目錄,我想在其中存儲對內置對象原型的任何更改。每個修改過的內置對象都有自己的文件(即custom/String.js,對String.prototype進行任何修改)。導出/導入ES6中內置對象的自定義功能?

除了這些文件,我還將有一個名爲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

所以你要導出和修改原型? – Li357

+0

@AndrewLi是的,非常。基本上我想將更改導入到字符串的原型中,而不是函數本身(如果甚至可能的話) – sookie

+0

@Rajesh號。這是更加奇怪的用法。 – Bergi

回答

6

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