xxxxxxxxxx
class Students {
name;
address;
schoolName = "Morning Sun School";
constructor(name,address,fatherName){
this.name = name;
this.address = address;
this.fatherName = fatherName;
}
raceCompetition(){
console.log(this.name, "start to run")
}
}
const olivia = new Students("Olivia", "USA","James");
const emma = new Students("Emma", "UK", "alex");
console.log(olivia);
olivia.raceCompetition()
console.log(emma);
emma.raceCompetition()
// output of olivia
Students {
name: 'Olivia',
address: 'USA',
schoolName: 'Morning Sun School',
fatherName: 'James'
}
Olivia start to run
// output of Emma
Students {
name: 'Emma',
address: 'UK',
schoolName: 'Morning Sun School',
fatherName: 'alex'
}
Emma start to run
xxxxxxxxxx
class ClassMates{
constructor(name,age){
this.name=name;
this.age=age;
}
displayInfo(){
return this.name + "is " + this.age + " years old!";
}
}
let classmate = new ClassMates("Mike Will",15);
classmate.displayInfo(); // result: Mike Will is 15 years old!
xxxxxxxxxx
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
present = () => { //or present(){
console.log(`Hi! i'm ${this.name} and i'm ${this.age} years old`)
}
}
let me = new Person("tsuy", 15);
me.present();
// -> Hi! i'm tsuy and i'm 15 years old.
xxxxxxxxxx
// Improved formatting of Spotted Tailed Quoll's answer
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
introduction() {
return `My name is ${name} and I am ${age} years old!`;
}
}
let john = new Person("John Smith", 18);
console.log(john.introduction());
xxxxxxxxxx
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
// Getter
get area() {
return this.calcArea();
}
// Method
calcArea() {
return this.height * this.width;
}
}
const square = new Rectangle(10, 10);
console.log(square.area); // 100
xxxxxxxxxx
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
carDetails() {
console.log("Car name: " + this.name, "\nCar year: "+ this.year);
}
drive() {
console.log("Driving!")
}
}
let car = new Car("Audi", 2019);
car.carDetails();
car.drive();
xxxxxxxxxx
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
get area() {
return this.calcArea();
}
calcArea() {
return this.height * this.width;
}
}
const square = new Polygon(10, 10);
console.log(square.area);
xxxxxxxxxx
function Animal (name) {
this.name = name;
}
Animal.prototype.speak = function () {
console.log(`${this.name} makes a noise.`);
}
class Dog extends Animal {
speak() {
console.log(`${this.name} barks.`);
}
}
let d = new Dog('Mitzie');
d.speak(); // Mitzie barks.
// For similar methods, the child's method takes precedence over parent's method
xxxxxxxxxx
/* A class is a blue print that you create objects from*/
/* How to create a class in javascript in it's simplest form*/
class Fruit{
}
var apple =new Fruit ();
xxxxxxxxxx
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
displayInfo() {
return this.name + ' is' + this.age + " years old";
}
}
const Anthony = new Person('Anthony', 32);