node.js classes code example
Example 1: es6 class example
<script>
class Student {
constructor(rno,fname,lname){
this.rno = rno
this.fname = fname
this.lname = lname
console.log('inside constructor')
}
set rollno(newRollno){
console.log("inside setter")
this.rno = newRollno
}
}
let s1 = new Student(101,'Sachin','Tendulkar')
console.log(s1)
s1.rollno = 201
console.log(s1)
</script>
Example 2: javascript class
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());
Example 3: javascript classes
class MyClass {
constructor(FirstProperty, SecondProperty, etcetera) {
this.firstProperty = FirstProperty;
this.secondProperty = SecondProperty;
}
method(Parameters) {
}
get getBothValues()
{
return [firstProperty, secondProperty];
}
}
Example 4: node js class
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
Rectangle.count++;
}
get area() {
return this.calcArea();
}
calcArea() {
return this.height * this.width;
}
static calcArea(width, height) {
return width * height;
}
}
Rectangle.count = 0;
const square = new Rectangle(10, 10);
console.log(square.height, square.width);
console.log(square.area);
console.log(square.calcArea());
console.log(Rectangle.count);
console.log(Rectangle.calcArea(15, 15));
Example 5: es6 class example
<script>
class Student {
constructor(rno,fname,lname){
this.rno = rno
this.fname = fname
this.lname = lname
console.log('inside constructor')
}
get fullName(){
console.log('inside getter')
return this.fname + " - "+this.lname
}
}
let s1 = new Student(101,'Sachin','Tendulkar')
console.log(s1)
console.log(s1.fullName)
</script>
Example 6: class declaration in javascript
class NameOfClass {
obj="text";
obj2="some other text";
}
console.log(new NameOfClass);