目前使用vite搭建项目工具库,发现一些知识盲区ESECMAScript模块,目前模块解决方案,使用importexport管理依赖browser这种写法可以直接通过加载AMD的方式,全称AsynchronousModuleDefinition,异步模块加载机制iife因为我们的应用程序可能包含许多来自不同源文件的函数和全局变量,所以限制全局变量的数量很重要。如果我们有一些不需要再次使用的启动代码,我们可以使用IIFE模式。因为我们不会再次重用代码,在这种情况下使用IIFE比使用函数声明或函数表达式更好constmakeWithdraw=(balance)=>((copyBalance)=>{letbalance=copyBalance;//ThisvariableisprivateconstdoBadThings=()=>{console.log('我会用你的钱做坏事');};doBadThings();return{withdraw(amount){if(balance>=amount){balance-=amount;returnbalance;}return'Insufficientmoney';},};})(balance);constfirstAccount=makeWithdraw(100);//“我会用你的钱做坏事”console.log(firstAccount.balance);//undefinedconsole.log(firstAccount.withdraw(20));//80console.log(firstAccount.withdraw(30));//50console.log(firstAccount.doBadThings);//不明确的;这个方法是privateconstsecondAccount=makeWithdraw(20);//“我会用你的钱做坏事”console.log(secondAccount.withdraw(30));//“资金不足”console.log(secondAccount.wit画图(20));//0
