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!
// 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());
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
/* 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 ();
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
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
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
class MyArray extends Array {
// Overwrite species to the parent Array constructor
static get [Symbol.species]() { return Array; }
}
let a = new MyArray(1,2,3);
let mapped = a.map(x => x * x);
console.log(mapped instanceof MyArray); // false
console.log(mapped instanceof Array); // true
/*
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.
*/
//Class
class Person{
constructor(){
this.name = 'Bill Gates';
}
}
var person1 = new Person();
console.log(person1.name); //Bill Gates
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
const Animal = {
speak() {
console.log(`${this.name} makes a noise.`);
}
};
class Dog {
constructor(name) {
this.name = name;
}
}
// If you do not do this you will get a TypeError when you invoke speak
Object.setPrototypeOf(Dog.prototype, Animal);
let d = new Dog('Mitzie');
d.speak(); // Mitzie makes a noise.
//class in es6 are just functional constructor.
class PersonES6{
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= new Person('Shirshak','Kandel',25)
shirshakES6.aboutPerson();
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
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
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
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
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
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
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
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
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
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
// Defining class using es6
class Vehicle {
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 constructor
let bike1 = new Vehicle('Hayabusa', 'Suzuki', '1340cc');
let bike2 = new Vehicle('Ninja', 'Kawasaki', '998cc');
console.log(bike1.name); // Hayabusa
console.log(bike2.maker); // Kawasaki
console.log(bike1.getDetails());
const p = new Rectangle(); // ReferenceError
class Rectangle {}
// functions can be called even before they are defined, but classes must be defined before they can be constructed.
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
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
class Person
{
constructor(name)
{
this.name = name;
console.log("HELLO WORLD")
/*console.log will show HELLO WORLD everytime an instance of Person is created*/
}
}
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);
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