问题
Below is the code am trying the figure out, when arrays (bill, tipValue, totalAmmount) are declared within the object method i get "cannot set property '0' undefined error. But, When the same arrays are declared outside the object then i get expected result"
code am getting exception:
var john = {
bill: [124, 48, 268, 180, 42],
tipValue: [],
totalAmmount: [],
calcTip() {
this.bill.forEach(function (ele, i) {
this.tipValue[i] = innercalc(ele);
this.totalAmmount[i] = this.tipValue[i] + ele;
}); //not working code
function innercalc(value) {
if (value < 50)
return value * 0.2;
else if (value > 50 && value < 200)
return value * 0.15;
else if (value > 200)
return value * 0.1;
}
}
}
john.calcTip();
console.log(john.tipValue);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="codingchallange5.js"></script>
</body>
</html>
Uncaught Exception: Cannot set Property '0' of undefined
Code which is working:
var tipValue = [];
var totalAmmount = [];
var john = {
bill: [124, 48, 268, 180, 42],
// tipValue: [],
// totalAmmount: [],
calcTip() {
this.bill.forEach(function (ele, i) {
tipValue[i] = innercalc(ele);
totalAmmount[i] = tipValue[i] + ele;
}); //not working code
function innercalc(value) {
if (value < 50)
return value * 0.2;
else if (value > 50 && value < 200)
return value * 0.15;
else if (value > 200)
return value * 0.1;
}
}
}
john.calcTip();
console.log(tipValue);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="codingchallange5.js"></script>
</body>
</html>
回答1:
Use arrow function. It will not mess with this
and everything will work as you expected:
var john = {
bill: [124, 48, 268, 180, 42],
tipValue: [],
totalAmmount: [],
calcTip() {
this.bill.forEach((ele, i) => { // replace with arrow function here
this.tipValue[i] = innercalc(ele);
this.totalAmmount[i] = this.tipValue[i] + ele;
});
function innercalc(value) {
if (value < 50)
return value * 0.2;
else if (value > 50 && value < 200)
return value * 0.15;
else if (value > 200)
return value * 0.1;
}
}
}
john.calcTip();
console.log(john.tipValue);
From MDN: An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this
, arguments
, super
, or new.target
keywords.
回答2:
When you used this in function forEach(), it is undefined by default as you did not pass any reference argument. You need to pass a reference to john to forEach()!
this.bill.forEach(function (ele, i) {
this.tipValue[i] = innercalc(ele);
this.totalAmmount[i] = this.tipValue[i] + ele;
}, this);
array.forEach(function(currentValue, index, arr), thisValue)
thisValue:
- Optional. A value to be passed to the function to be used as its "this" value.
- If this parameter is empty, the value "undefined" will be passed as its "this" value.
JavaScript Array forEach() Method
来源:https://stackoverflow.com/questions/60574254/how-to-fix-uncaught-typeerror-cannot-set-property-0-of-undefined