简介:
模块化就是js与js之间的调用,类似于Java类与类之间的调用。使用模块化可以使项目结构更加清晰明了,代码更加优雅。
es5写法:
demo1.js
//定义方法 const hello=function(){
return "hello"; } const ok=function(){
return "ok"; } //指定哪些方法可以被调用 module.exports={
hello, ok }
demo2.js
//引入要调用的文件 const m=require('./demo1.js') console.log(m.hello()); console.log(m.ok());
es6写法(需要Babel转成es5才能运行):
demo1.js 定义被调用方法
export default{
hello(){
return "hello"; }, ok(){
return "ok"; } }
demo2.js 引入文件,调用方法。
import m from './demo1.js' console.log(m.hello()); console.log(m.ok());
进入根目录使用Babel 转码,具体使用请看上一篇博客
babel es6 -d es6_1

进入转码后的目录运行代码:

帮助到您请点赞关注收藏谢谢!!
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/220141.html原文链接:https://javaforall.net
