Javascript 如何在js中使用外部js文件?

Javascript 如何在js中使用外部js文件?,javascript,json,include,Javascript,Json,Include,我想缩短我的js中的行数。我将json放在文件顶部。我想把它存储在一个单独的文件中。在php中,您只需要执行一个include语句,但对于js,是否有类似的内容?有几种方法可以在js文件之间进行通信。 在es5中 //FOR EXPORT use module.exports eg. module.exports= any content(function, object, array). //FOR IMPORT use require method eg. const xyz = re

我想缩短我的js中的行数。我将json放在文件顶部。我想把它存储在一个单独的文件中。在php中,您只需要执行一个include语句,但对于js,是否有类似的内容?

有几种方法可以在js文件之间进行通信。 在es5中

//FOR EXPORT use module.exports

eg. module.exports= any content(function, object, array).

//FOR IMPORT use require method

eg. const xyz = require('.path_to_your_file) //.js extension is optional

//now exported content will be available in xyz.
es6中,我们有命名导出默认导出

// FOR **default export** use export default 
// eg. export default (any content [array,object,function])
// NOTE:- you can have only one default export in a file

// FOR named export use export 
// eg. export (any content [array,object,function])
// NOTE:- you can have multiple export in a file

///####################################
// FOR importing **default exported** content use following syntax
// import ABC from 'source file';

 //now exported content will be available in ABC.

// FOR importing **named exported** content use following syntax
// import {exported_name} from 'source file'; // see object destruct in es6 for detail
// as we can have multiple named export we can import multiple content using comma separated syntax and using.
// import { export1,export2} from 'source file';
您还可以将所有命名的导出合并为一个导入名称,如下所示

//import * as ABC from 'source file';

在这里,所有命名的导出将在ABC对象中可用,您可以通过点或括号表示法进行访问。

这是否回答了您的问题?