【发布时间】:2020-10-30 17:10:22
【问题描述】:
我很困惑如何编辑我的代码,以便我可以拥有至少一种使用参数(或返回值)的方法,而不仅仅是使用 console.log。我很感激你们可以提供的任何帮助或建议谢谢! :) 这是我的代码:
class Planet{
constructor(name, numberOfMoons, size) {
this.name = name;
this.numberOfMoons = numberOfMoons;
this.size = size;
}
orbit(){
console.log(this.name + ' is a planet and therefore orbits around the sun.');
}
}
//inheritance class
class DwarfPlanet extends Planet{
constructor(name, numberOfMoons, size, orbitalNeighbourhood) {
super(name, numberOfMoons, size);
this.orbital = orbitalNeighbourhood;
}
getOrbital(){
console.log(this.name + " is a dwarf planet because it doesn't have a clear orbital neighnourhood ");
}
}
let earth = new Planet('Earth', 1 , 6371);
earth.orbit();
let pluto = new DwarfPlanet("Pluto", 5 , 1188, 'Kuiper Belt');
pluto.getOrbital();
//Array of Objects
var stars = [
{
name: 'Sun',
temperature: 5778,
colour: 'White'
},
{
name: 'Pistol',
temperature: 11800,
colour: 'Blue'
},
{
name: "Antares",
temperature: 3500,
colour: "Red"
}
];
console.log("Fun Fact: the biggest star isn't the sun, instead it is a blue star called 'Pistol'. Here's some information about it: ");
console.log(stars[1]);
【问题讨论】:
-
将
console.log()替换为return,后跟您希望返回的任何值,如return this.name + ' is a planet and therefore orbits around the sun.'; -
哦,好的,谢谢!非常!有没有其他方法比如添加新数据什么的?
-
也知道“参数”和“返回值”是两个非常不同的东西。 “参数”是传递给函数的内容,然后在整个函数处理过程中用作局部变量。 “返回值”是您希望函数返回给调用者的任何值。
-
我不确定我是否理解您的问题.. 还有其他返回数据的方法吗?唯一的另一种方法是让函数重新分配一个范围比函数更高的变量的值。
-
你会如何建议我有一个利用参数的方法呢?
标签: javascript class parameters return-value