class properties javascript code example
Example 1: javascript class
class ClassMates{
constructor(name,age){
this.name=name;
this.age=age;
}
displayInfo(){
return this.name + "is " + this.age + " years old!";
}
}
let classmate = new ClassMates("Mike Will",15);
classmate.displayInfo();
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 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 4: javascript classes
class MyClass {
constructor(FirstProperty, SecondProperty, etcetera) {
this.firstProperty = FirstProperty;
this.secondProperty = SecondProperty;
}
method(Parameters) {
}
get getBothValues()
{
return [firstProperty, secondProperty];
}
}
Example 5: 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 6: javascript create class
class Car {
constructor(brand) {
this.carname = brand;
}
}