[英]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.