现代 JavaScript 特性详解
1. 函数参数处理:ES5 与 ES6 的对比
在处理函数参数时,ES5 和 ES6 有明显的差异。以下是一个计算税收的示例:
// ES5 版本
function calcTaxES5(income) {
var customers = Array.prototype.slice.call(arguments, 1);
console.log("ES5. Calculating tax for customers with the income " + income);
customers.forEach(function (customer) {
console.log("Processing ", customer);
});
}
calcTaxES5(50000, "Smith", "Johnson", "McDonald");
calcTaxES5(750000, "Olson", "Clinton");
// ES6 和剩余参数运算符
function calcTaxES6(income, ...customers) {
console.log(`ES6. Calculating tax for customers with the income ${income}`);
customers.forEach((customer) => console.log(`Processing ${customer}`));
}
calcTaxES6(50000, "Smith", "Johnson", "McDona
超级会员免费看
订阅专栏 解锁全文
1258

被折叠的 条评论
为什么被折叠?



