数据类型的转换 - Javascript
Conversion of datatypes - Javascript
我对如何在下面的代码中实现不同数据类型的转换感到困惑。转换包括(int转string、string转int、float转int等)我老师说当我是reading/writing个文件的时候这很容易做到,但我还是一头雾水。我将不胜感激任何帮助或建议,谢谢!这是我的代码:
//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);
读取和写入文件的最直接方法是使用它们的同步副本: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()
数据到 readPlanets()
中的 Planet
个实例。 JSON 本身不知道 class - JSON.parse()
创建的对象是基本的 JavaScript 对象。
另请注意我是如何稍微更改您的数据格式的。我没有使用像 'Twenty-seven moons'
或 '6,371 km'
这样的值,而是在 JSON 中放置了一个简单的数字:27
和 6371
。那是因为您通常希望尽可能保持原始数据,因为显示格式是您可能希望随时更改的内容。数据应该持久存在。如果您想将这些原始数字转换为您的格式,我建议您将方法添加到 Planet
class 到 return 卫星的格式化数字和大小。
我对如何在下面的代码中实现不同数据类型的转换感到困惑。转换包括(int转string、string转int、float转int等)我老师说当我是reading/writing个文件的时候这很容易做到,但我还是一头雾水。我将不胜感激任何帮助或建议,谢谢!这是我的代码:
//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);
读取和写入文件的最直接方法是使用它们的同步副本: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()
数据到 readPlanets()
中的 Planet
个实例。 JSON 本身不知道 class - JSON.parse()
创建的对象是基本的 JavaScript 对象。
另请注意我是如何稍微更改您的数据格式的。我没有使用像 'Twenty-seven moons'
或 '6,371 km'
这样的值,而是在 JSON 中放置了一个简单的数字:27
和 6371
。那是因为您通常希望尽可能保持原始数据,因为显示格式是您可能希望随时更改的内容。数据应该持久存在。如果您想将这些原始数字转换为您的格式,我建议您将方法添加到 Planet
class 到 return 卫星的格式化数字和大小。