Recently delving into JavaScript, I encountered an error while running the code below. I attempted to create a class and instantiate objects from it.
- Computer.js
constructor(
// defining parameters
name,
modelName,
sizeInInches,
color,
type,
generation,
clockSpeed,
ramSize,
diskSize,
diskType
) {
// defining properties
this.name = name;
this.modelName = modelName;
this.sizeInInches = sizeInInches;
this.color = color;
this.type = type;
this.processorSpecs = {
generation: generation,
clockSpeed: clockSpeed,
type: type,
};
this.ramSize = ramSize;
this.diskType = diskType;
this.diskSize = diskSize;
}
outputConsole() {
console.log(this.name, this.ramSize, this.color, this.diskSize);
}
}
export default Computer;
- Script.js
import Computer from "./Computer.js";
const myComp = new Computer(
"Pranav's HP Laptop",
"HP-envym6-1225dx",
15,
"Grey",
"Intel i5",
"3rd-Generation",
"2.2GHz",
"8.0 GB",
"HDD",
"750.0 GB"
);
console.log("Object created\n", myComp);
console.log("Method output\n", myComp.outputConsole());
console.log("Program Finished");
- index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Practice: Making classes and objects</title>
<script type="module" src="Computer.js"></script>
<script type="module" src="script.js"></script>
</head>
<body></body>
</html>
[Viewing the obtained output][1] [1]: https://i.sstatic.net/8afX0.png
Why is myComp.outputConsole()
displayed before "Output the method\n"
in the line
console.log("Output the method\n", myComp.outputConsole());
?
Please guide me on where I might be mistaken. Thanks in advance! :)