已编辑解决:问题是inNum称为action(ac)=> (ac作为参考)为了打电话给所需的案例,论证应该是' ac' =>(一个字符串)。谢谢@floor!
在作业中,我构建了一个计算器,一个函数正在使用另一个在其下面声明的函数来清除条目
function inNum(num) {
if(arrHistory.indexOf('=') != -1) {
action(ac);
}
(entry.length === 1 && entry[0] === 0) ? entry[0] = num: typeof entry[0] == 'string' ? entry = [num] : entry.push(num);
return $('#entry')[0].textContent = entry.join('');
}
function action(action) {
switch(action) {
case 'ac':
arrHistory = [];
entry = [0];
return $('#history')[0].textContent = arrHistory.join(''),
$('#entry')[0].textContent = entry.join('');
似乎inNum无法访问操作。为什么? Aren是全局范围内的两个函数(inNum和action),因此在运行时可以相互访问吗?
我尝试通过在包含我所有代码的对象中将函数编写为方法来解决此问题,但是inNum仍然无法使用参数AC调用操作。 我知道简单的解决方案是为switch(ac)编写case而不是调用函数,但我有兴趣理解这种行为背后的规则。
var entry = [0];
var arrHistory = [];
function inNum(num) {
if(arrHistory.indexOf('=') != -1) {
action(ac);
}
(entry.length === 1 && entry[0] === 0) ? entry[0] = num: typeof entry[0] == 'string' ? entry = [num] : entry.push(num);
return $('#entry')[0].textContent = entry.join('');
}
function action(action) {
switch(action) {
case 'ac':
arrHistory = [];
entry = [0];
return $('#history')[0].textContent = arrHistory.join(''), $('#entry')[0].textContent = entry.join('');
case 'ce':
if(typeof arrHistory[arrHistory.length - 1] == 'string') {
//if (typeof Number(arrHistory[arrHistory.length - 1]) != 'number')
arrHistory.pop();
entry = [0];
$('#entry')[0].textContent = entry.join('');
}
else {
arrHistory = [];
entry = [0];
return $('#history')[0].textContent = arrHistory.join(''),
$('#entry')[0].textContent = entry.join('');
}
return $('#history')[0].textContent = arrHistory.join('');
case 'divide':
if(typeof entry[0] === 'number' && typeof entry[entry.length - 1] === 'number') {
arrHistory.push(Number(entry.join('')));
arrHistory.push('\/');
entry = [String.fromCharCode(247)];
$('#entry')[0].textContent = entry.join('');
return $('#history')[0].textContent = arrHistory.join('');
}
break;
case 'multiply':
if(typeof entry[0] === 'number' && typeof entry[entry.length - 1] === 'number') {
arrHistory.push(Number(entry.join('')));
arrHistory.push('\*');
entry = ['X'];
$('#entry')[0].textContent = entry.join('');
return $('#history')[0].textContent = arrHistory.join('');
}
break;
case 'add':
if(typeof entry[0] === 'number' && typeof entry[entry.length - 1] === 'number') {
arrHistory.push(Number(entry.join('')));
arrHistory.push('\+');
entry = ['\+'];
$('#entry')[0].textContent = entry.join('');
return $('#history')[0].textContent = arrHistory.join('');
}
break;
case 'subtract':
if(typeof entry[0] === 'number' && typeof entry[entry.length - 1] === 'number') {
arrHistory.push(Number(entry.join('')));
arrHistory.push('\-');
entry = ['\-'];
$('#entry')[0].textContent = entry.join('');
return $('#history')[0].textContent = arrHistory.join('');
}
break;
case 'calculate':
// MULTIPLICATION AND DIVISION METHOD
function mulDiv(acumulator, element, index, arr) {
if(arr[index + 1] == '*' || arr[index + 1] == '/') {
if(index < arr.length - 2) {
let mulDivSum;
switch(arr[index + 1]) {
case '*':
mulDivSum = element * arr[index + 2]
arr.splice(index, 2);
break;
case '/':
mulDivSum = element / arr[index + 2]
arr.splice(index, 2);
break;
}
acumulator.push(mulDivSum);
return acumulator;
}
}
else {
acumulator.push(element);
return acumulator
}
}
// ADDITION AND SUBTRACTION METHOD
function addSub(sum, element, index, arr) {
switch(element) {
case "+":
if(index < arr.length - 1) {
sum += arr[index + 1];
break;
}
case "-":
if(index < arr.length - 1) {
sum -= arr[index + 1];
break;
}
}
return sum;
}
if((entry.length === 1 && typeof entry[0] == 'number') || (entry[entry.length - 1] == 'number')) {
arrHistory.push(Number(entry.join('')));
let result = arrHistory.reduce(mulDiv, [])
.reduce(addSub, arrHistory[0]);
arrHistory.push('=');
arrHistory.push(result)
$('#history')[0].textContent = arrHistory.join('');
$('#entry')[0].textContent = Number(result);
}
break;
case 'decimal':
if(entry.indexOf('.') === -1) {
entry.push('\.');
$('#entry')[0].textContent = entry.join('');
}
}
}
&#13;