I'm not an expert, but import is a lot like require , but the key difference is:
you can import custom elements with import (suppose it's close to python), but with require you only export one module as a namespace, the rest is submodules.
second, require more from node.js thingy (although you can bring it to the browser using a browser), but import now a native function of ES6, that is, browsers that support ES6, import will work
An example from lukehoban es6features to reapply my first point:
// lib/math.js export function sum(x, y) { return x + y; } export var pi = 3.141593; // app.js import * as math from "lib/math"; alert("2π = " + math.sum(math.pi, math.pi)); // otherApp.js import {sum, pi} from "lib/math"; alert("2π = " + sum(pi, pi)); //Some additional features include export default and export *: // lib/mathplusplus.js export * from "lib/math"; export var e = 2.71828182846; export default function(x) { return Math.log(x); } // app.js import ln, {pi, e} from "lib/mathplusplus"; alert("2π = " + ln(e)*pi*2);
source share