Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript class

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!
Comment

javascript create class

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.
Comment

javascript classes

class Car {
  constructor(name, year) {
    this.name = name;
    this.year = year;
  }

  carDetails() {
    console.log("Car name: " + this.name, "
Car year: "+ this.year);
  }

  drive() {
    console.log("Driving!")
  }

}

let car = new Car("Audi", 2019);
car.carDetails();
car.drive();
Comment

javascript class

// 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());
Comment

javascript class

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
Comment

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
Comment

define a class in javascript

/* 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 ();
Comment

class javascript

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);
Comment

how to create a class javascript

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);
Comment

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
Comment

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
Comment

javascript create class

class Car {
  constructor(brand) {
    this.carname = brand;
  }
}
Comment

class in javascript

class Students {
    name;
    address;
    schoolName = "Morning Sun School";
    constructor(name,address,fatherName){
        this.name = name;
        this.address = address;
        this.fatherName = fatherName;
    }
}
const olivia = new Students("Olivia", "USA","James");
const emma = new Students("Emma", "UK", "alex");
console.log(olivia);
console.log(emma);

// Output of Olivia
Students {
    name: 'Olivia',
    address: 'USA',
    schoolName: 'Morning Sun School',
    fatherName: 'James'
  }
// Output of Emma
  Students {
    name: 'Emma',
    address: 'UK',
    schoolName: 'Morning Sun School',
    fatherName: 'alex'
  }
Comment

class in javascript

/*
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
Comment

js class syntax

// 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(); 
Comment

class in javascript

class Support {
    name;
    designation;
    address;
    constructor(name, designation, address) {
        this.name = name;
        this.designation = designation;
        this.address = address;
    }
    startSession() {
        console.log("start a support session");
    }
}

const abraham = new Support("Abraham", "Web developer", "US");
console.log(abraham);
//output:
/* Support {
    name: 'Abraham',
    designation: 'Web developer',
    address: 'US'
  } */
Comment

javascript classes

class SayHelloTo {
  name (to) {
    console.log(`Hello ${to}`);
  }
  constructor (to) {
    this.name(to);
  }
}
const helloWorld = new SayHelloTo(World);
Comment

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
Comment

JavaScript Classes

class Car {
  constructor(name, year) {
    this.name = name;
    this.year = year;
  }
}
Comment

Creating JavaScript Class

// constructor function
function Person () {
    this.name = 'John',
    this.age = 23
}

// create an object
const person1 = new Person();
Comment

javascript classes

//private but is a good example
class includes {
    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);
    }
  }
Comment

class in javascript

//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();
Comment

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
Comment

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
Comment

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
Comment

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
Comment

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
Comment

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
Comment

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
Comment

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
Comment

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
Comment

JavaScript Classes

class Car {
  constructor(name, year) {
    this.name = name;
    this.year = year;
  }
}
Comment

javascript classes

let Person = class {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
}
Comment

javascript class

class Rectangle {
  constructor(hauteur, largeur) {
    this.hauteur = hauteur;
    this.largeur = largeur;
  }
}
Comment

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
Comment

class in js

// 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());
Comment

how to define class in javascript

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.
Comment

classes in js

javascript class is like a template for Objects.
Comment

creating JS classes

// creating a class
class Person {
  constructor(name) {
    this.name = name;
  }
}
Comment

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
Comment

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
Comment

Create A Class Using JavaScript


	 class Person
	 {
	 	
		constructor(name)
		 {
			 this.name = name;
			 console.log("HELLO WORLD")
             /*console.log will show HELLO WORLD everytime an instance of Person is created*/
		 }
	 }
Comment

JavaScript Classes

class Person {
  constructor(name) {
    this.name = name;
  }
}
Comment

class declaration in javascript

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);
Comment

javascript class

// unnamed
let Rectangle = class {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};
console.log(Rectangle.name);
// output: "Rectangle"

// named
let Rectangle = class Rectangle5 {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};
console.log(Rectangle.name);
// output: "Rectangle2"
Comment

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
Comment

class in javascript

class Students {
    name;
    address;
    schoolName = "Morning Sun School";
    constructor(name,address,fatherName){
        this.name = name;
        this.address = address;
        this.fatherName = fatherName;
    }
}
const olivia = new Students("Olivia", "USA","James");
const emma = new Students("Emma", "UK", "alex");
console.log(olivia);
console.log(emma);

// Output of Olivia
Students {
    name: 'Olivia',
    address: 'USA',
    schoolName: 'Morning Sun School',
    fatherName: 'James'
  }
// Output of Emma
  Students {
    name: 'Emma',
    address: 'UK',
    schoolName: 'Morning Sun School',
    fatherName: 'alex'
  }
Comment

PREVIOUS NEXT
Code Example
Javascript :: simple todo list javascript 
Javascript :: how to connect a swagger ui express 
Javascript :: data attribute hide & show function syntax in jquery 
Javascript :: how to get checkbox value in jquery 
Javascript :: jQuery Method Chaining 
Javascript :: javascript pad string left 
Javascript :: NodeJS Content-Type 
Javascript :: array.sort 
Javascript :: vue3 header 
Javascript :: redux action creators 
Javascript :: how to make a 3*3 grid using html,css and javascript 
Javascript :: what is closure 
Javascript :: Use the parseInt Function 
Javascript :: array class javascript 
Javascript :: UnhandledPromiseRejectionWarning 
Javascript :: run function after another function javascript 
Javascript :: make a component update every second react 
Javascript :: pass function with parameter as prop 
Javascript :: using server passed values and client js together in ejs 
Javascript :: crud in node 
Javascript :: mdn trimstart 
Javascript :: vanilla tilt js 
Javascript :: array indexof 
Javascript :: can we add string and int in javascript 
Javascript :: modal javascript 
Javascript :: events 
Javascript :: set tiemzone datetime object 
Javascript :: javascript delete element of an array 
Javascript :: nodejs: express: package for Router 
Javascript :: javascript return function 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =