我知道在es6中这是如何制作一个块范围。
'use strict';
{
let account = [1678922, 300450];
let compute = (credit, debit) => {
return credit - debit;
}
console.log(compute(...account));
}
ES5 [这是我的期望]
(function(){
var account = [1678922, 300450];
var compute = function compute(credit, debit) {
return credit - debit;
};
console.log(compute.apply(undefined, account));
})();
console.log(user1);
BABEL编译
'use strict';
{
var account = [1678922, 300450];
var compute = function compute(credit, debit) {
return credit - debit;
};
console.log(compute.apply(undefined, account));
}
我想删除编译版本中的全局变量。
问题:
任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
babel编译的例子;继续评论:
编辑:基于您的例子
预编译的:
{
let account = [1678922, 300450];
let compute = (credit, debit) => {
return credit - debit;
}
console.log(compute(...account));
}
{
let account = [16789, 3004];
let compute = (credit, debit) => {
return credit - debit;
}
console.log(compute(...account));
}
Postcompiled:
{
var account = [1678922, 300450];
var compute = function compute(credit, debit) {
return credit - debit;
};
console.log(compute.apply(undefined, account));
}
{
var _account = [16789, 3004];
var _compute = function _compute(credit, debit) {
return credit - debit;
};
console.log(_compute.apply(undefined, _account));
}
从本质上讲,没关系。 Babel正在以一种无关紧要的方式智能地编译它。