关于'es' | 'cjs' | 'umd' | 'iife' 相关介绍

目前在用vite构建项目工具库发现一些知识盲区

ES

ECMAScript Module,现在使用的模块方案,使用 import export 来管理依赖
浏览器直接通过 的方式加载

AMD,全称是Asynchronous Module Definition,即异步模块加载机制

iife

因为我们的应用程序可能包含来自不同源文件的许多函数和全局变量,所以限制全局变量的数量很重要。如果我们有一些不需要再次使用的启动代码,我们可以使用 IIFE 模式。由于我们不会再次重用代码,因此在这种情况下使用 IIFE 比使用函数声明或函数表达式更好

const makeWithdraw = (balance) => ((copyBalance) => {
  let balance = copyBalance; // This variable is private
  const doBadThings = () => {
    console.log('I will do bad things with your money');
  };
  doBadThings();
  return {
    withdraw(amount) {
      if (balance >= amount) {
        balance -= amount;
        return balance;
      }
      return 'Insufficient money';
    },
  };
})(balance);

const firstAccount = makeWithdraw(100);   // "I will do bad things with your money"
console.log(firstAccount.balance);        // undefined
console.log(firstAccount.withdraw(20));   // 80
console.log(firstAccount.withdraw(30));   // 50
console.log(firstAccount.doBadThings);    // undefined; this method is private
const secondAccount = makeWithdraw(20);   // "I will do bad things with your money"
console.log(secondAccount.withdraw(30));  // "Insufficient money"
console.log(secondAccount.withdraw(20));  // 0

你可能感兴趣的:(关于'es' | 'cjs' | 'umd' | 'iife' 相关介绍)