class variable javascript code example
Example 1: 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 2: 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 3: how to create instance of class in javascript
class Hello {
person(name){
this.name = name;
}
constructor(name){
this.person(name);
console.log(`Hello ${this.name}`);
}
}
const helloInstance = new Hello('john doe');
Example 4: variables in js class
constructor(){
this.foo = bar
}
Example 5: how to create class in javascript
class ClassName
{
}