Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,24 @@
//Constructor function.
function Car(brand, weight) {
this.brand = brand;
this.weight = weight || 1000; // Resort to default value (with 'or' notation).
}
Car.prototype.getPrice = function() { // Method of Car.
return this.price;
}
function Truck(brand, size) {
this.car = Car;
this.car(brand, 2000); // Call another function, modifying the "this" object (e.g. "superconstructor".)
this.size = size; // Custom property for just this object.
}
Truck.prototype = Car.prototype; // Also "import" the prototype from Car.
var cars = [ // Some example car objects.
new Car("Mazda"),
new Truck("Volvo", 2)
];
for (var i=0; i<cars.length; i++) {
alert(cars[i].brand + " " + cars[i].weight + " " + cars[i].size + ", " +
(cars[i] instanceof Car) + " " + (cars[i] instanceof Truck));
}

View file

@ -0,0 +1,77 @@
class Car {
/**
* A few brands of cars
* @type {string[]}
*/
static brands = ['Mazda', 'Volvo'];
/**
* Weight of car
* @type {number}
*/
weight = 1000;
/**
* Brand of car
* @type {string}
*/
brand;
/**
* Price of car
* @type {number}
*/
price;
/**
* @param {string} brand - car brand
* @param {number} weight - mass of car
*/
constructor(brand, weight) {
if (brand) this.brand = brand;
if (weight) this.weight = weight
}
/**
* Drive
* @param distance - distance to drive
*/
drive(distance = 10) {
console.log(`A ${this.brand} ${this.constructor.name} drove ${distance}cm`);
}
/**
* Formatted stats string
*/
get formattedStats() {
let out =
`Type: ${this.constructor.name.toLowerCase()}`
+ `\nBrand: ${this.brand}`
+ `\nWeight: ${this.weight}`;
if (this.size) out += `\nSize: ${this.size}`;
return out
}
}
class Truck extends Car {
/**
* Size of truck
* @type {number}
*/
size;
/**
* @param {string} brand - car brand
* @param {number} size - size of car
*/
constructor(brand, size) {
super(brand, 2000);
if (size) this.size = size;
}
}
let myTruck = new Truck('Volvo', 2);
console.log(myTruck.formattedStats);
myTruck.drive(40);