165 lines
3.6 KiB
JavaScript
165 lines
3.6 KiB
JavaScript
//my first objectsű
|
|
|
|
const person = {
|
|
firstname: "Spongebob",
|
|
lastname: "Squarepants",
|
|
age: 30,
|
|
isemployed: true,
|
|
sayHello: function(){
|
|
console.log("HELOOOOOOO Im sponge!")
|
|
},
|
|
sayBye : () => console.log("BBBBYYEEEEEEEEEE!!!")
|
|
};
|
|
|
|
console.log(person.age);
|
|
console.log(person.firstname);
|
|
console.log(person.lastname);
|
|
person.sayHello();
|
|
person.sayBye();
|
|
|
|
//The "this" keyword!!!
|
|
const person2 = {
|
|
firstname: "Spongebob",
|
|
lastname: "Squarepants",
|
|
age: 30,
|
|
isemployed: true,
|
|
sayHello: function(){
|
|
console.log(`HELOOOOOOO I am ${this.firstname}!`)
|
|
},
|
|
sayBye : () => console.log(`BBBBYYEEEEEEEEEE I was ${this.firstname}`), // "this" CANT BE USED WITH ARROW FUNCTIONT!!!!
|
|
sayBye1 : () => console.log(`BBBBYYEEEEEEEEEE I was ${person2.firstname}`),
|
|
sayBye2 : () => console.log(`BBBBYYEEEEEEEEEE I was ${firstname}`), //WRONG!!!!!!!!
|
|
};
|
|
|
|
person2.sayHello();
|
|
person2.sayBye();
|
|
person2.sayBye1();
|
|
//person2.sayBye2();
|
|
|
|
|
|
function car(make, model, year, color){
|
|
this.make = make,
|
|
this.model = model,
|
|
this.year = year,
|
|
this.color = color,
|
|
this.drive = function(){
|
|
console.log(`You drive a ${this.make}!!`)
|
|
}
|
|
};
|
|
|
|
const car1 = new car("Tesla", "Y", 2015, "black");
|
|
const car2 = new car("Ford", "Mustang", 2020, "red");
|
|
|
|
console.log(car1.color);
|
|
|
|
const carList = []; //ObjectArray
|
|
carList.push(car1);
|
|
carList.push(car2);
|
|
|
|
console.log(carList);
|
|
carList[0].drive();
|
|
|
|
//CLASSES!!!!!!!!!!
|
|
|
|
class Products{
|
|
static tax = 1.27;
|
|
constructor(name, price){
|
|
this.name = name;
|
|
this.price = price;
|
|
}
|
|
|
|
displayProduct(){
|
|
console.log(`Product name: ${this.name}`);
|
|
}
|
|
|
|
displayPrice(){
|
|
console.log(`Theprice: ${this.price}`);
|
|
}
|
|
|
|
calculateTotal(){
|
|
console.log(`The taxed price will be: ${this.price * Products.tax}`);
|
|
}
|
|
}
|
|
|
|
const shirt = new Products("Shirt", 19.99);
|
|
const pants = new Products("Pants", 22.50);
|
|
const underwear = new Products("Underwear", 122.50);
|
|
|
|
underwear.displayProduct();
|
|
underwear.displayPrice();
|
|
underwear.calculateTotal();
|
|
|
|
//https://youtu.be/lfmg-EJ8gm4?si=KJ_EBH2FxCEtonyR&t=19910
|
|
|
|
|
|
|
|
//INHERITANCE
|
|
|
|
class Animals{
|
|
alive = true
|
|
constructor(name, age){
|
|
this.name = name;
|
|
this.age = age;
|
|
}
|
|
|
|
eat(){
|
|
console.log(`This ${this.name} is eating`);
|
|
}
|
|
|
|
sleep(){
|
|
console.log(`This ${this.name} is sleeping`);
|
|
}
|
|
move(){
|
|
console.log(`The ${this.name} is mowing with ${this.speed} km/h!!`)
|
|
}
|
|
}
|
|
|
|
class Rabbit extends Animals{
|
|
constructor(name, age, speed){
|
|
super(name, age);
|
|
this.speed = speed;
|
|
}
|
|
run(){
|
|
console.log(`This ${this.name} is running`);
|
|
}
|
|
set sname(newName){
|
|
this._name = newName
|
|
}
|
|
get _name(){
|
|
return this._name
|
|
}
|
|
}
|
|
|
|
class Fish extends Animals{
|
|
constructor(name, age, speed){
|
|
super(name, age);
|
|
this.speed = speed;
|
|
}
|
|
swim() {
|
|
console.log(`This ${this.name} is swimming like a rabbit, hopping through the water!`);
|
|
}
|
|
}
|
|
|
|
class Hawk extends Animals{
|
|
constructor(name, age, speed){
|
|
super(name, age); //BEFORE constructing the child we NEED to construct the parent with the super() function!!!
|
|
this.speed = speed;
|
|
}
|
|
}
|
|
|
|
let fish = new Fish("izé", 4, 25);
|
|
const rabbit = new Rabbit("nyuszi",5 ,23 );
|
|
const hawk = new Hawk("Man", 5, 80);
|
|
|
|
console.log(fish.alive)
|
|
fish.eat();
|
|
rabbit.sleep();
|
|
rabbit.run();
|
|
hawk.sleep();
|
|
console.log(hawk.speed);
|
|
|
|
rabbit.move();
|
|
fish.move();
|
|
hawk.move();
|
|
|