Question #227   Submitted by Answiki on 11/25/2020 at 08:02:28 PM UTC

How to define a class in JavaScript ?

Answer   Submitted by Answiki on 11/25/2020 at 08:29:43 PM UTC

There are two ways to define classes in JavaScript :


Method 1. Class declaration

// Class rectangle
class Rectangle {

	// Constructor
	constructor(height,width)  {
		this.height = height;
		this.width 	= width;
	}

	// Method
	area() {
		return this.width * this.height;
	}
}


Method 2. Class expression

// Class rectangle
let Rectangle = class {

	// Constructor
	constructor(height,width)  {
		this.height = height;
		this.width 	= width;
	}

	// Method
	area() {
		return this.width * this.height;
	}
}


Class instantiation

Whatever the method used to define the class, the class instantiation is done with the new keyword:

const rectangle = new Rectangle(100,200);

6 events in history
Answer by Answiki on 11/25/2020 at 08:29:43 PM

There are two ways to define classes in JavaScript :


Method 1. Class declaration

// Class rectangle
class Rectangle {

	// Constructor
	constructor(height,width)  {
		this.height = height;
		this.width 	= width;
	}

	// Method
	area() {
		return this.width * this.height;
	}
}


Method 2. Class expression

// Class rectangle
let Rectangle = class {

	// Constructor
	constructor(height,width)  {
		this.height = height;
		this.width 	= width;
	}

	// Method
	area() {
		return this.width * this.height;
	}
}


Class instantiation

Whatever the method used to define the class, the class instantiation is done with the new keyword:

const rectangle = new Rectangle(100,200);

Answer by Answiki on 11/25/2020 at 08:24:30 PM

There are two ways to define classes in JavaScript :


Method 1. Class declaration

// Class rectangle
class Rectangle {

	// Constructor
	constructor(height,width)  {
		this.height = height;
		this.width 	= width;
	}

	// Method
	area() {
		return this.width * this.height;
	}
}


Method 2. Class expression

// Class rectangle
let Rectangle = class {

	// Constructor
	constructor(height,width)  {
		this.height = height;
		this.width 	= width;
	}

	// Method
	area() {
		return this.width * this.height;
	}
}


Class instantiation

Whatever the method used to define the class, the class instantiation is done with the new keyword:

const rectangle = new Rectangle(100,200);

Answer by Answiki on 11/25/2020 at 08:23:06 PM

There are two ways to define classes in JavaScript :


Method 1. Class declaration

// Class rectangle
class Rectangle {

	// Constructor
	constructor(height,width)  {
		this.height = height;
		this.width 	= width;
	}

	// Method
	area() {
		return this.width * this.height;
	}
}


Method2. Class expressions

// Class rectangle
let Rectangle = class {

	// Constructor
	constructor(height,width)  {
		this.height = height;
		this.width 	= width;
	}

	// Method
	area() {
		return this.width * this.height;
	}
}


Class instantiation

Whatever the method used to define the class, the class instantiation is done with the new keyword:

const rectangle = new Rectangle(100,200);

Answer by Answiki on 11/25/2020 at 08:21:05 PM

There are two ways to define classes in JavaScript :


Method 1. Class declaration

// Class rectangle
class Rectangle {

	// Constructor
	constructor(height,width)  {
		this.height = height;
		this.width 	= width;
	}

	// Method
	area() {
		return this.width * thiw.height;
	}
}


Method2. Class expressions

// Class rectangle
let Rectangle = class {

	// Constructor
	constructor(height,width)  {
		this.height = height;
		this.width 	= width;
	}

	// Method
	area() {
		return this.width * thiw.height;
	}
}


Class instantiation

Whatever the method used to define the class, the class instantiation is done with the new keyword:

const rectangle = new Rectangle(100,200);

Answer by Answiki on 11/25/2020 at 08:20:42 PM

There are two ways to define classes in JavaScript :


Class declaration

// Class rectangle
class Rectangle {

	// Constructor
	constructor(height,width)  {
		this.height = height;
		this.width 	= width;
	}

	// Method
	area() {
		return this.width * thiw.height;
	}
}


Class expressions

// Class rectangle
let Rectangle = class {

	// Constructor
	constructor(height,width)  {
		this.height = height;
		this.width 	= width;
	}

	// Method
	area() {
		return this.width * thiw.height;
	}
}


Class instantiation

Whatever the method used to define the class, the class instantiation is done with the new keyword:

const rectangle = new Rectangle(100,200);

Question by Answiki 11/25/2020 at 08:02:28 PM
How to define a class in JavaScript ?
# ID Query URL Count

Icons proudly provided by Friconix.