class in es6 code example
Example 1: es6 class example
'use strict'
class Polygon {
constructor(height, width) {
this.h = height;
this.w = width;
}
test() {
console.log("The height of the polygon: ", this.h)
console.log("The width of the polygon: ",this. w)
}
}
var polyObj = new Polygon(10,20);
polyObj.test();
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')
}
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 3: es6 class example
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
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);