WeChat Mini Programs introduces other methods of js. What is the difference between using require and import?

for example, when my md5 encryption js is placed in the utils folder, and the js of the page needs to be introduced into md5, what is the difference between using require introduction and import introduction? I think the official document says to use require to introduce, I look at other people"s Mini Program code, and sometimes use import to introduce js.


the difference between import and require
the most important idea in node programming is modularity. Both import and require are used by modularization.

follow the specification
require is how the AMD specification is introduced
import is a syntax standard of es6, which must be converted to the syntax of es5 if you want to be compatible with browsers

call time
require is a run-time call, so require can theoretically be used anywhere in the code
import is called at compile time, so it must be placed at the beginning of the file

essence
require is an assignment process. In fact, the result of require is an object, number, string, function, etc., and then assigning the result of require to a variable
import is a deconstruction process, but at present, all engines have not implemented import,. We use babel in node to support ES6, and only transcode ES6 to ES5 for re-execution, and import syntax will be transcoded to require

.
There is no difference between

require and import . These are just two js modularization specifications. If your Mini Program uses babel compilation, you can write either. If not, use the require statement, which is supported natively by nodejs

.

js modularization

1.ES2015 import statement

import App from './App';

import 'moment';

2.CommonJS require () statement (supported by nodejs)

const App = require('./App');

require('moment')

3.AMD define and require statements (supported by requirejs)
requirejs is a library that implemented js modularization earlier, but it is hardly used now. It's probably written this way

require(['jquery'], function(Lib){
    // do sth
});

http://es6.ruanyifeng.com/-sharpdo.

Menu