declare function in class javascript code example
Example 1: javascript new function as class
function Person(name){
const birth = new Date();
this.greet = () => `Hello my name is ${name}. I was born at ${birth.toISOString()}`;
}
const joe = new Person("Joe");
joe.greet();
Example 2: 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 3: javascript create class
class Car {
constructor(brand) {
this.carname = brand;
}
}
Example 4: es6 class example
var Polygon = class {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
Example 5: class declaration in javascript
class NameOfClass {
obj="text";
obj2="some other text";
}
console.log(new NameOfClass);