Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

class in javascript


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
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #class #javascript
ADD COMMENT
Topic
Name
4+7 =