Example
javascript
export const bundleType = () => process.env.BUNDLE || 'dev';
这是一个 ES6 模块导出 + 箭头函数的组合语法
目录
[1. 箭头函数基本语法](#1. 箭头函数基本语法)
[2. 你的代码逐层解析](#2. 你的代码逐层解析)
[3. 具体功能解释](#3. 具体功能解释)
[4. 箭头函数的特点](#4. 箭头函数的特点)
[5. 类似实用模式](#5. 类似实用模式)
[6. 实际应用扩展](#6. 实际应用扩展)
1. 箭头函数基本语法
基本形式
javascript
// 标准函数
function add(a, b) {
return a + b;
}
// 箭头函数
const add = (a, b) => {
return a + b;
};
简化形式
javascript
// 1. 单参数可省略括号
const square = x => x * x;
// 2. 无参数需要括号
const getTime = () => Date.now();
// 3. 单行表达式可隐式返回(省略花括号和 return)
const double = x => x * 2; // 隐式返回
// 4. 多行需要花括号和显式 return
const calculate = (a, b) => {
const sum = a + b;
return sum * 2;
};
2. 你的代码逐层解析
javascript
export const bundleType = () => process.env.BUNDLE || 'dev';
分解步骤:
javascript
// 第1层:模块导出
export const bundleType = ...;
// 第2层:箭头函数定义
const bundleType = () => ...;
// 第3层:函数体(单行表达式)
process.env.BUNDLE || 'dev';
// 第4层:逻辑或运算符
// 如果 process.env.BUNDLE 有值(非假值),就返回它
// 否则返回 'dev'
等价转换:
javascript
// 等价于普通函数
export const bundleType = function() {
return process.env.BUNDLE || 'dev';
};
// 等价于多行箭头函数
export const bundleType = () => {
return process.env.BUNDLE || 'dev';
};
3. 具体功能解释
作用:
获取环境变量 BUNDLE的值,如果不存在则返回默认值 'dev'
使用场景:
javascript
// 假设在命令行设置环境变量
// Mac/Linux: BUNDLE=production npm start
// Windows: set BUNDLE=production && npm start
// 使用时:
console.log(bundleType()); // 如果设置了 BUNDLE=production,输出 'production'
// 如果没有设置,输出默认值 'dev'
// 常见应用:
if (bundleType() === 'production') {
// 生产环境逻辑
enableProductionMode();
} else {
// 开发环境逻辑
enableDevTools();
}
4. 箭头函数的特点
优势:
-
简洁语法:单行函数特别简洁
-
隐式返回:单行表达式自动返回结果
-
无 this 绑定 :箭头函数没有自己的
this,继承外层作用域 -
无 arguments 对象 :使用 rest 参数
(...args)替代
比较示例:
javascript
// 传统函数
const obj1 = {
value: 10,
getValue: function() {
return this.value; // this 指向 obj1
}
};
// 箭头函数
const obj2 = {
value: 10,
getValue: () => {
return this.value; // this 指向外层作用域(可能是 window/undefined)
}
};
5. 类似实用模式
模式1:配置获取函数
javascript
export const getConfig = () => process.env.CONFIG || 'default';
export const isProduction = () => process.env.NODE_ENV === 'production';
export const getApiUrl = () => process.env.API_URL || 'http://localhost:3000';
模式2:条件函数
javascript
// 根据环境返回不同函数
export const getLogger = () =>
process.env.NODE_ENV === 'production'
? (msg) => console.log(`[PROD] ${msg}`)
: (msg) => console.log(`[DEV] ${msg}:`, new Date());
// 使用
const logger = getLogger();
logger('应用启动');
模式3:链式配置
javascript
export const getSettings = () => ({
env: process.env.NODE_ENV || 'development',
bundle: process.env.BUNDLE || 'dev',
debug: process.env.DEBUG === 'true',
// 默认值可以是计算值
maxConnections: process.env.MAX_CONN || (process.env.NODE_ENV === 'production' ? 100 : 10)
});
6. 实际应用扩展
完整配置模块示例:
// config.js
export const bundleType = () => process.env.BUNDLE || 'dev';
export const isDevelopment = () => bundleType() === 'dev';
export const isProduction = () => bundleType() === 'production';
export const isStaging = () => bundleType() === 'staging';
export const config = {
apiBaseUrl: isProduction()
? 'https://api.company.com'
: isStaging()
? 'https://staging.company.com'
: 'http://localhost:8080',
features: {
enableAnalytics: isProduction(),
enableDebugTools: !isProduction(),
cacheTimeout: isProduction() ? 3600 : 60
}
};
在构建工具中使用:
// webpack.config.js
import { bundleType } from './config.js';
module.exports = {
mode: bundleType() === 'production' ? 'production' : 'development',
devtool: bundleType() === 'production' ? false : 'eval-source-map',
output: {
filename: bundleType() === 'production'
? '[name].[contenthash].js'
: '[name].js'
}
};
总结
你的代码 :export const bundleType = () => process.env.BUNDLE || 'dev';
-
导出 :
export const- 导出常量供其他模块使用 -
函数定义 :
() =>- 无参数的箭头函数 -
函数体 :
process.env.BUNDLE || 'dev'- 单行表达式,隐式返回 -
逻辑:获取环境变量,有值返回值,无值返回默认值 'dev'
-
目的:创建一个灵活的配置获取函数,根据环境变量动态决定行为
箭头函数在此处的优势 :简洁明了,适合这种简单的取值+默认值逻辑,避免了不必要的 function和 return关键字。