-
Notifications
You must be signed in to change notification settings - Fork 3.9k
relearning javascript #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
bug fixes |
| * - Create a class describing this object type - its properties and methods. | ||
| * - Create several objects using the class. | ||
| * - Test the objecs by calling their properties and using their methods in the console. | ||
| */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this example, I'll use a Car class that has properties like make, model, year, and methods like startEngine and stopEngine.
Car Class Definition
class Car {
constructor(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
this.isEngineOn = false;
}
startEngine() {
if (!this.isEngineOn) {
this.isEngineOn = true;
console.log(`${this.make} ${this.model} engine started.`);
} else {
console.log(`${this.make} ${this.model} engine is already on.`);
}
}
stopEngine() {
if (this.isEngineOn) {
this.isEngineOn = false;
console.log(`${this.make} ${this.model} engine stopped.`);
} else {
console.log(`${this.make} ${this.model} engine is already off.`);
}
}
}Creating Objects and Testing
Now, let's create several objects using the Car class and test them by accessing their properties and using their methods.
// Creating car objects
const car1 = new Car("Toyota", "Corolla", 2020);
const car2 = new Car("Honda", "Civic", 2019);
const car3 = new Car("Ford", "Mustang", 2021);
// Accessing properties
console.log(`${car1.make} ${car1.model} (${car1.year})`);
console.log(`${car2.make} ${car2.model} (${car2.year})`);
console.log(`${car3.make} ${car3.model} (${car3.year})`);
// Testing methods
car1.startEngine();
car1.stopEngine();
car2.startEngine();
car2.startEngine(); // Trying to start the engine again
car2.stopEngine();
car3.startEngine();
car3.stopEngine();
car3.stopEngine(); // Trying to stop the engine again
// Output for each method call is printed to the consoleExplanation:
-
Class Definition:
- The
Carclass has a constructor that initializes themake,model,year, andisEngineOnproperties. - The
startEnginemethod checks if the engine is off and starts it if true, otherwise, it indicates that the engine is already on. - The
stopEnginemethod checks if the engine is on and stops it if true, otherwise, it indicates that the engine is already off.
- The
-
Creating Objects:
- We create three instances (
car1,car2,car3) of theCarclass, each representing a different car.
- We create three instances (
-
Accessing Properties:
- We print out the
make,model, andyearproperties of each car object to the console.
- We print out the
-
Testing Methods:
- We test the
startEngineandstopEnginemethods on each car object. The console output will show the engine status and handle cases where the engine is already in the desired state.
- We test the
This demonstrates the basics of object-oriented programming in JavaScript using a class to create and manage objects, as well as how to work with their properties and methods.
| * - Create properties to describe the objects and set their values. | ||
| * - Find an object that has another object inside of it to create a nested object. | ||
| * - Test your objects in the browser console by accessing the entire object and its specific properties. | ||
| */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const house = {
year: "1973",
make: "Duplex",
color: "Cream",
model: {
Side1: "two bedroom two bath",
Side2: "one bedroom one bath"
},
houseOpen: false,
newModel: function(Right, Left) {
this.model.Side1 = Right;
this.model.Side2 = Left;
}
};
// Accessing the entire object and specific properties
console.log(house);
console.log("Year:", house.year);
console.log("Make:", house.make);
console.log("Color:", house.color);
console.log("Model (before):", house.model);
console.log("Model Side1:", house.model.Side1);
console.log("Model Side2:", house.model.Side2);
// Using the newModel method to update the model
house.newModel("three bedroom three bath", "two bedroom one bath");
// Checking the updates
console.log("Model (after):", house.model);
No description provided.