-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
relearning javascript #76
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,15 @@ | |
* - 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 commentThe reason will be displayed to describe this comment to others. Learn more. For this example, I'll use a Car Class Definitionclass 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 TestingNow, let's create several objects using the // 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 console Explanation:
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. |
||
|
||
import Backpack from "../../03_12/Backpack"; | ||
|
||
const everydayPack = Backpack( | ||
"Mikey", | ||
24, | ||
"black", | ||
12, | ||
15, | ||
15, | ||
"true" | ||
); |
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);