I know classes do not exist in javascript.
But, Is there any indirect way to create a class in javascript?
what is the use of defining a class in javascript
I know classes do not exist in javascript.
But, Is there any indirect way to create a class in javascript?
what is the use of defining a class in javascript
JavaScript classes, introduced in ECMAScript 2015, are primarily syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax is not introducing a new object-oriented inheritance model to JavaScript. JavaScript classes provide a much simpler and clearer syntax to create objects and deal with inheritance.
Here are two examples using ES5 and ES6
'use strict';
/**
* Shape class.
*
* @constructor
* @param {String} id - The id.
* @param {Number} x - The x coordinate.
* @param {Number} y - The y coordinate.
*/
function Shape(id, x, y) {
this.id = id;
this.setLocation(x, y);
}
/**
* Set shape location.
*
* @param {Number} - The x coordinate.
* @param {Number} - The y coordinate.
*/
Shape.prototype.setLocation = function(x, y) {
this.x = x;
this.y = y;
};
/**
* Get shape location.
*
* @return {Object}
*/
Shape.prototype.getLocation = function() {
return {
x: this.x,
y: this.y
};
};
/**
* Get shape description.
*
* @return {String}
*/
Shape.prototype.toString = function() {
return 'Shape("' + this.id + '")';
};
/**
* Circle class.
*
* @constructor
* @param {String} id - The id.
* @param {Number} x - The x coordinate.
* @param {Number} y - The y coordinate.
* @param {Number} radius - The radius.
*/
function Circle(id, x, y, radius) {
Shape.call(this, id, x, y);
this.radius = radius;
}
Circle.prototype = Object.create(Shape.prototype);
Circle.prototype.constructor = Circle;
/**
* Get circle description.
*
* @return {String}
*/
Circle.prototype.toString = function() {
return 'Circle > ' + Shape.prototype.toString.call(this);
};
// test the classes
var myCircle = new Circle('mycircleid', 100, 200, 50); // create new instance
console.log(myCircle.toString()); // Circle > Shape("mycircleid")
console.log(myCircle.getLocation()); // { x: 100, y: 200 }
'use strict';
/**
* Shape class.
*
* @constructor
* @param {String} id - The id.
* @param {Number} x - The x coordinate.
* @param {Number} y - The y coordinate.
*/
class Shape(id, x, y) {
constructor(id, x, y) { // constructor syntactic sugar
this.id = id;
this.setLocation(x, y);
}
/**
* Set shape location.
*
* @param {Number} - The x coordinate.
* @param {Number} - The y coordinate.
*/
setLocation(x, y) { // prototype function
this.x = x;
this.y = y;
}
/**
* Get shape location.
*
* @return {Object}
*/
getLocation() {
return {
x: this.x,
y: this.y
};
}
/**
* Get shape description.
*
* @return {String}
*/
toString() {
return `Shape("${this.id}")`;
}
}
/**
* Circle class.
*
* @constructor
* @param {String} id - The id.
* @param {Number} x - The x coordinate.
* @param {Number} y - The y coordinate.
* @param {Number} radius - The radius.
*/
function Circle extends Shape {
constructor(id, x, y, radius) {
super(id, x, y); // call Shape's constructor via super
this.radius = radius;
}
/**
* Get circle description.
*
* @return {String}
*/
toString() { // override Shape's toString
return `Circle > ${super.toString()}`; // call `super` instead of `this` to access parent
}
}
// test the classes
var myCircle = new Circle('mycircleid', 100, 200, 50); // create new instance
console.log(myCircle.toString()); // Circle > Shape("mycircleid")
console.log(myCircle.getLocation()); // { x: 100, y: 200 }
Use this example.
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
get area() {
return this.calcArea();
}
calcArea() {
return this.height * this.width;
}
}
const square = new Rectangle(10, 10);
console.log(square.area);
So basically, you can make classes, and they need to start with an "init" function which is usually the constructor function. You can add as many functions after that. NOTE: The code above was from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes