xxxxxxxxxx
class NameOfClass {
//class declaration first letter should be capital it's a convention
obj="text";
obj2="some other text";
}
//always call class with "new" key word
console.log(new NameOfClass);
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
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
/* 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);
xxxxxxxxxx
// constructor function
function Person () {
this.name = 'John',
this.age = 23
}
// create an object
const person1 = new Person();
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
// method 1
function nested(name , age , color){
this.name = name;
this.details = {
age : age,
color : color
}
}
let nestedObj = new nested( "Elroi" , 22 , "blue");
console.log(nestedObj)
// method 2
class Nested2{
constructor(name , age , color){
this.name = name;
this.details = {
age : age,
color : color
}
};
displayInfo(){
console.log(`${this.name} ${this.details.age} ${this.details.color} `)
}
}
let aaa = new Nested2("Ean" , 14 , "black");
aaa.displayInfo();
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 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