猿问

数据类型的转换 - Javascript

我对如何在下面的代码中实现不同数据类型的转换感到困惑。转换包括(int 到 string、string 到 int、float 到 int 等)我的老师说当我读/写文件时这可以很容易地完成,但我仍然很困惑。我将不胜感激任何帮助或建议,谢谢!这是我的代码:


//files

var fs = require("fs");

//reading files

fs.readFile('sources.txt', (err, data) => {

  console.log("File output: " + data.toString());

  //writing files

      fs.writeFile('written.txt',data,(err, result) => {

        if(err) console.log('error', err);

      });

}); 


// planet class

class Planet{

    constructor(name, numberOfMoons, size) {

        this.name = name;

        this.numberOfMoons = numberOfMoons;

        this.size = size;

    }

    orbit(){

        //return value

        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 value

        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());


//Array of Objects (anonymous option)

var stars = [

    {

        name: 'Sun',

        temperature: 5778,

        colour: 'White'

    },

    {

        name: 'Pistol',

        temperature: 11800,

        colour: 'Blue'

    },

    {

        name: "Antares",

        temperature: 3500,

        colour: "Red"

    }

];


// Array of Objects (using Planet Class)


var planets = [

  new Planet('Earth', 'One moon', '6,371 km'),

  new Planet('Mars', 'Two mooons', '3,389 km'),

  new Planet('Uranus', 'Twenty-seven moons', '25,362 km'),

  new Planet('Saturn', 'Fifty-three moons', '58,232 km'),

];



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('Here are some planets and their properties:');

console.log(planets);


Cats萌萌
浏览 103回答 1
1回答

慕尼黑8549860

读写文件最直接的方法是使用它们的同步对应项:readFileSync()和writeFileSync()。为了巧妙地使用 JSON,您可以将函数定义为:const filePath = 'planets.json';function readPlanets() {    const json = fs.readFileSync(filePath);    const planets = JSON.parse(json);    return planets.map(planet => new Planet(        planet.name,        planet.numberOfMoons,        planet.size    ));}function writePlanets(planets) {    const json = JSON.stringify(planets, null, 4);    fs.writeFileSync(filePath, json);}然后,您可以填充您的planets.json文件:const planets = [    new Planet('Earth', 1, 6371),    new Planet('Mars', 2, 3389),    new Planet('Uranus', 27, 25362),    new Planet('Saturn', 53, 58232),];writePlanets(planets);要读回它,您可以使用以下readPlanets()函数:const storedPlanets = readPlanets();请注意我如何手动map()向. JSON 本身不知道类 - 创建的对象是基本的 JavaScript 对象。PlanetreadPlanets()JSON.parse()另请注意我如何稍微更改了您的数据格式。我没有使用像'Twenty-seven moons'or这样的值,而是在 JSON: and'6,371 km'中放入一个简单的数字。这是因为您通常希望尽可能保持原始数据,因为显示格式是您可能希望随时更改的内容。数据应该持续存在。如果您想将这些原始数字转换为您的格式,我建议您在类中添加方法以返回卫星的格式化数字和大小。276371Planet
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答