【问题标题】:Method with parameter or return value - Javascript带有参数或返回值的方法 - Javascript
【发布时间】: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


【解决方案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"));

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

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

如果我是你,我会查看类似 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))
})

【讨论】:

  • 谢谢你,我真的很感激,但我还没有讨论过任何与 JSON 相关的内容。是否有另一种方法可以执行您刚才所做的读取或写入不同文件的操作?
  • 另外,代码是否与Node兼容,因为我只是尝试了第一部分并且它不起作用?
  • 是的,它与节点兼容,哪个部分坏了?星的数据类型也是 JSON 对象。 { name: "Antares", temperature: 3500, colour: "Red" } 那是 JSON。在上面的编辑器中似乎也运行良好。
  • 在文件方面,您是在问如何在 Node.js 中读取文件?
  • 帮自己一个忙,尽可能远离 W3 学校。众所周知,信息不完整、过时或完全不正确。坚持使用 MDN。
猜你喜欢
  • 1970-01-01
  • 2013-05-09
  • 2015-12-04
  • 1970-01-01
  • 2012-03-20
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多