繁体   English   中英

带有参数或返回值的方法 - Javascript

[英]Method with parameter or return value - Javascript

我对如何编辑我的代码感到困惑,以便我可以拥有至少一种使用参数(或返回值)而不仅仅是 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返回的值。 如果你愿意,你可以两者都做。

对于方法,您可以以任何方式传递参数。 例如

 const func = (param1, param2) => { return `This is Param 1: ${param1} This is Param 2: ${param2}` } // This function returns a String console.log(func("Hi", "Hello"));

因此,对于您对类所做的事情,您只需像任何其他普通函数一样将参数传递到方法中即可。 我更新了您的代码示例,以包含如何使用星数组执行此操作。

我还更新了实例方法,使它们返回实际值。

如果我是你,我会查看类似这篇 W3 文章的内容: https : //www.w3schools.com/js/js_functions.asp

还有这篇关于类的 MDN 文章: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

只是为了打好基础。

祝你好运

 class Planet{ constructor(name, numberOfMoons, size) { this.name = name; this.numberOfMoons = numberOfMoons; this.size = size; } orbit(){ return `${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(){ return `${this.name} is a dwarf planet because it doesn't have a clear orbital neighnourhood "` } } let earth = new Planet('Earth', 1 , 6371); console.log(earth.orbit()); let pluto = new DwarfPlanet("Pluto", 5 , 1188, 'Kuiper Belt'); console.log(pluto.getOrbital()); class Stars { constructor() { this.stars = []; } addStars(star){ this.stars.push(star); } addMultipleStars(stars){ this.stars = [...this.stars, ...stars] } allStars(){ return this.stars; } } //Array of Objects var stars = [ { name: 'Sun', temperature: 5778, colour: 'White' }, { name: 'Pistol', temperature: 11800, colour: 'Blue' }, { name: "Antares", temperature: 3500, colour: "Red" } ]; let startContainer = new Stars(); startContainer.addMultipleStars(stars); const allStars = startContainer.allStars(); allStars.forEach(star => { console.log(JSON.stringify(star)) })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM