classClassMates{constructor(name,age){this.name=name;this.age=age;}displayInfo(){returnthis.name+"is "+this.age+" years old!";}}let classmate =newClassMates("Mike Will",15);
classmate.displayInfo();// result: Mike Will is 15 years old!
classPerson{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 =newPerson("tsuy",15);
me.present();// -> Hi! i'm tsuy and i'm 15 years old.
// Improved formatting of Spotted Tailed Quoll's answerclassPerson{constructor(name, age){this.name= name;this.age= age;}introduction(){return`My name is ${name} and I am ${age} years old!`;}}let john =newPerson("John Smith",18);console.log(john.introduction());
classStudents{
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 =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);
olivia.raceCompetition()console.log(emma);
emma.raceCompetition()// output of oliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}Olivia start to run
// output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}Emma start to run
/* A class is a blue print that you create objects from*//* How to create a class in javascript in it's simplest form*/classFruit{}var apple =newFruit();
functionAnimal(name){this.name= name;}Animal.prototype.speak=function(){console.log(`${this.name} makes a noise.`);}classDogextendsAnimal{speak(){console.log(`${this.name} barks.`);}}let d =newDog('Mitzie');
d.speak();// Mitzie barks.// For similar methods, the child's method takes precedence over parent's method
classStudents{
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 =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);
olivia.raceCompetition()console.log(emma);
emma.raceCompetition()// output of oliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}Olivia start to run
// output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}Emma start to run
classStudents{
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 =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);
olivia.raceCompetition()console.log(emma);
emma.raceCompetition()// output of oliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}Olivia start to run
// output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}Emma start to run
classMyArrayextendsArray{// Overwrite species to the parent Array constructorstaticget[Symbol.species](){returnArray;}}let a =newMyArray(1,2,3);let mapped = a.map(x=> x * x);console.log(mapped instanceofMyArray);// falseconsole.log(mapped instanceofArray);// true
classStudents{
name;
address;
schoolName ="Morning Sun School";constructor(name,address,fatherName){this.name= name;this.address= address;this.fatherName= fatherName;}}const olivia =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);console.log(emma);// Output of OliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}// Output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}
/*
A class is a function of a factory that will be used to create many objects,
but no code will be duplicated in creating each object. In other words,
the code will be written only once, using that code we can create a lot of
objects again and again.
*///ClassclassPerson{constructor(){this.name='Bill Gates';}}var person1 =newPerson();console.log(person1.name);//Bill Gates
classStudents{
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 =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);
olivia.raceCompetition()console.log(emma);
emma.raceCompetition()// output of oliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}Olivia start to run
// output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}Emma start to run
constAnimal={speak(){console.log(`${this.name} makes a noise.`);}};classDog{constructor(name){this.name= name;}}// If you do not do this you will get a TypeError when you invoke speakObject.setPrototypeOf(Dog.prototype,Animal);let d =newDog('Mitzie');
d.speak();// Mitzie makes a noise.
//private but is a good exampleclassincludes{constructor(){}inculde_apps(file){var script =document.createElement('script');
script.src="ext/apps/"+file;
script.type='text/javascript';document.getElementsByTagName('body').item(0).appendChild(script);}inculde_scripts(file){var script =document.createElement('script');
script.src="ext/scripts/"+file;
script.type='text/javascript';document.getElementsByTagName('body').item(0).appendChild(script);}inculde_css(file){var script =document.createElement('link');
script.rel="stylesheet";
script.type='text/css';
script.href="css/"+file;document.getElementsByTagName('head').item(0).appendChild(script);}}
//class in es6 are just functional constructor.classPersonES6{constructor(firstname,lastname,age){this.firstname= firstname;this.lastname=lastname;this.age=age
}aboutPerson(){console.log(`My name is ${this.firstname}${this.lastname} and I am ${this.age} years old`)}}const shirshakES6=newPerson('Shirshak','Kandel',25)
shirshakES6.aboutPerson();
classStudents{
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 =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);
olivia.raceCompetition()console.log(emma);
emma.raceCompetition()// output of oliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}Olivia start to run
// output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}Emma start to run
classStudents{
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 =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);
olivia.raceCompetition()console.log(emma);
emma.raceCompetition()// output of oliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}Olivia start to run
// output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}Emma start to run
classStudents{
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 =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);
olivia.raceCompetition()console.log(emma);
emma.raceCompetition()// output of oliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}Olivia start to run
// output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}Emma start to run
classStudents{
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 =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);
olivia.raceCompetition()console.log(emma);
emma.raceCompetition()// output of oliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}Olivia start to run
// output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}Emma start to run
classStudents{
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 =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);
olivia.raceCompetition()console.log(emma);
emma.raceCompetition()// output of oliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}Olivia start to run
// output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}Emma start to run
classStudents{
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 =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);
olivia.raceCompetition()console.log(emma);
emma.raceCompetition()// output of oliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}Olivia start to run
// output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}Emma start to run
classStudents{
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 =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);
olivia.raceCompetition()console.log(emma);
emma.raceCompetition()// output of oliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}Olivia start to run
// output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}Emma start to run
classStudents{
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 =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);
olivia.raceCompetition()console.log(emma);
emma.raceCompetition()// output of oliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}Olivia start to run
// output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}Emma start to run
classStudents{
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 =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);
olivia.raceCompetition()console.log(emma);
emma.raceCompetition()// output of oliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}Olivia start to run
// output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}Emma start to run
classStudents{
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 =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);
olivia.raceCompetition()console.log(emma);
emma.raceCompetition()// output of oliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}Olivia start to run
// output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}Emma start to run
// Defining class using es6classVehicle{constructor(name, maker, engine){this.name= name;this.maker= maker;this.engine= engine;}getDetails(){return(`The name of the bike is ${this.name}.`)}}// Making object with the help of the constructorlet bike1 =newVehicle('Hayabusa','Suzuki','1340cc');let bike2 =newVehicle('Ninja','Kawasaki','998cc');console.log(bike1.name);// Hayabusaconsole.log(bike2.maker);// Kawasakiconsole.log(bike1.getDetails());
const p =newRectangle();// ReferenceErrorclassRectangle{}// functions can be called even before they are defined, but classes must be defined before they can be constructed.
classStudents{
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 =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);
olivia.raceCompetition()console.log(emma);
emma.raceCompetition()// output of oliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}Olivia start to run
// output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}Emma start to run
classStudents{
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 =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);
olivia.raceCompetition()console.log(emma);
emma.raceCompetition()// output of oliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}Olivia start to run
// output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}Emma start to run
classPerson{constructor(name){this.name= name;console.log("HELLO WORLD")/*console.log will show HELLO WORLD everytime an instance of Person is created*/}}
classNameOfClass{//class declaration first letter should be capital it's a convention
obj="text";
obj2="some other text";}//always call class with "new" key wordconsole.log(newNameOfClass);
classStudents{
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 =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);
olivia.raceCompetition()console.log(emma);
emma.raceCompetition()// output of oliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}Olivia start to run
// output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}Emma start to run
classStudents{
name;
address;
schoolName ="Morning Sun School";constructor(name,address,fatherName){this.name= name;this.address= address;this.fatherName= fatherName;}}const olivia =newStudents("Olivia","USA","James");const emma =newStudents("Emma","UK","alex");console.log(olivia);console.log(emma);// Output of OliviaStudents{name:'Olivia',address:'USA',schoolName:'Morning Sun School',fatherName:'James'}// Output of EmmaStudents{name:'Emma',address:'UK',schoolName:'Morning Sun School',fatherName:'alex'}