92 lines
2.2 KiB
JavaScript
92 lines
2.2 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
|