1

According to this question How do I include a JavaScript file in another JavaScript file?. It seems a lots of people are interested in breaking big Javascript projects into small modules and export/import modules for code reuse.

After some research, import/export are designed for this feature. According to the references, they're initially defined in ES6.

Update

Latest version of main browsers shipped with this feature implemented. To have the latest status, please always refer to the References.

(If you're using nodejs, Modules (https://nodejs.org/dist/latest-v5.x/docs/api/modules.html) is the best aproach)

Refrerences:

https://developer.mozilla.org/en/docs/web/javascript/reference/statements/import https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

  • related questions: http://stackoverflow.com/questions/33516906/which-browsers-support-import-and-export-syntax-for-ecmascript-6?rq=1, http://stackoverflow.com/questions/13355486/what-ecmascript-6-features-can-i-currently-use-in-browsers?lq=1 –  Mar 16 '16 at 05:32
  • **NOTE:** modules have as of the time of this comment shipped in chrome and safari. – Jared Smith Sep 18 '17 at 18:29
  • @JaredSmith Thanks for pointing out. It's a good news. Updated my answer and references are always the best place to check. –  Sep 30 '17 at 07:09

2 Answers2

0

In the current versions of JS there are a few methods of doing this. Require uses the AMD design pattern, and is the standard for frontend dependency injection / module loading. Frameworks such as Angular use this method.

Here is a link to the require docs. http://requirejs.org/

Joel
  • 1,309
  • 2
  • 10
  • 20
0

Time goes by... Today I would rather suggest using Babel (within browserify or webpack) to turn ES modules into plain old javascript.

Then you have the full power of import/export syntax.

Require/AMD/CommonJS is just getting deprecated anytime soon.

sibidiba
  • 6,270
  • 7
  • 40
  • 50