[英]JavaScript function returning undefined + console log
有人可以帮我修复我的千树()函数,它显示我想要的控制台日志,但它也在控制台上返回 undefined 。
我如何使该功能只执行控制台日志?
这是我正在学习的课程练习的一部分。
该功能就在代码的底部
谢谢你
// park constructor
class Park {
constructor (name, buildYear, trees, size) {
this.name = name;
this.buildYear = buildYear;
this.trees = trees;
this.size = size;
}
// number of tree / park area
treeDensity() {
return this.trees / this.size;
}
// return the park age
parkAge() {
return new Date().getFullYear() - this.buildYear;
}
}
// street constructor
class Street {
constructor (name, buildYear, size) {
this.name = name;
this.buildYear = buildYear;
this.size = size;
}
};
// all parks
let allParks = [
park1 = new Park("Brighton Park", 1900, 1200, 3),
park2 = new Park("Worthing Park", 1800, 650, 2),
park3 = new Park("Shoreham Park", 1850, 350, 2)
]
// all streets
let allStreets = [
street1 = new Street("Brighton Street", 1858, 2000),
street2 = new Street("Worthing Street", 1950, 1200),
street3 = new Street("Shoreham Street", 1850, 800),
street4 = new Street("Lancing Street", 1980, 760)
];
// calculate the average age of all parks
let parkAgeAvg = function() {
return (park1.parkAge() + park2.parkAge() + park3.parkAge()) / 3
};
// Display the name of the park with more than 1000 trees
let thousandTrees = function() {
for (let cur of allParks) {
if (cur.trees >= 1000) {
console.log(`${cur.name} has more than 1000 trees, we have verified there is actually ${cur.trees} trees in total.`);
}
}
};
// total length of streets and avg length
//size classification of all streets
在 chrome 中,我在 console.log 中没有看到任何错误
// park constructor class Park { constructor (name, buildYear, trees, size) { this.name = name; this.buildYear = buildYear; this.trees = trees; this.size = size; } // number of tree / park area treeDensity() { return this.trees / this.size; } // return the park age parkAge() { return new Date().getFullYear() - this.buildYear; } } // street constructor class Street { constructor (name, buildYear, size) { this.name = name; this.buildYear = buildYear; this.size = size; } }; // all parks let allParks = [ park1 = new Park("Brighton Park", 1900, 1200, 3), park2 = new Park("Worthing Park", 1800, 650, 2), park3 = new Park("Shoreham Park", 1850, 350, 2) ] // all streets let allStreets = [ street1 = new Street("Brighton Street", 1858, 2000), street2 = new Street("Worthing Street", 1950, 1200), street3 = new Street("Shoreham Street", 1850, 800), street4 = new Street("Lancing Street", 1980, 760) ]; // calculate the average age of all parks let parkAgeAvg = function() { return (park1.parkAge() + park2.parkAge() + park3.parkAge()) / 3 }; // Display the name of the park with more than 1000 trees let thousandTrees = function() { for (let cur of allParks) { if (cur.trees >= 300) { console.log(`${cur.name} has more than 1000 trees, we have verified there is actually ${cur.trees} trees in total.`); } } }; thousandTrees();
您看到的undefined
只是因为您在控制台中调用的函数返回undefined
,并且您在控制台中评估的任何 JS 都会显示表达式的值。 它与控制台日志代码无关。
旁注,您如何初始化事物存在问题:
let allParks = [
park1 = new Park("Brighton Park", 1900, 1200, 3),
park2 = new Park("Worthing Park", 1800, 650, 2),
park3 = new Park("Shoreham Park", 1850, 350, 2)
]
在某些模式下,JavaScript 会对此park1
窒息,因为park1
、 park2
和park3
是未定义的。 我相信你真正想要的只是:
let allParks = [
new Park("Brighton Park", 1900, 1200, 3),
new Park("Worthing Park", 1800, 650, 2),
new Park("Shoreham Park", 1850, 350, 2)
]
在前者中,您使用赋值表达式的结果填充数组。 在第二个中,你用构造的对象填充它。
allStreets
反馈相同。
定义您在数组中使用的变量:
// all parks
let park1,park2,park3;
let allParks = [
park1 = new Park("Brighton Park", 1900, 1200, 3),
park2 = new Park("Worthing Park", 1800, 650, 2),
park3 = new Park("Shoreham Park", 1850, 350, 2)
]
// all streets
let street1, street2, street3, street4;
let allStreets = [
street1 = new Street("Brighton Street", 1858, 2000),
street2 = new Street("Worthing Street", 1950, 1200),
street3 = new Street("Shoreham Street", 1850, 800),
street4 = new Street("Lancing Street", 1980, 760)
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.