亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

數據類型的轉換 - Javascript

數據類型的轉換 - Javascript

Cats萌萌 2023-06-29 22:29:34
我對如何在下面的代碼中實現不同數據類型的轉換感到困惑。轉換包括(int 到 string、string 到 int、float 到 int 等)我的老師說當我讀/寫文件時這可以很容易地完成,但我仍然很困惑。我將不勝感激任何幫助或建議,謝謝!這是我的代碼://filesvar fs = require("fs");//reading filesfs.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 classclass 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 classclass 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);
查看完整描述

1 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

讀寫文件最直接的方法是使用它們的同步對應項: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


查看完整回答
反對 回復 2023-06-29
  • 1 回答
  • 0 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號