classes and constructors in javascript code example
Example 1: javascript class constructor
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
const person = new Person("John Doe", 23);
console.log(person.name);
Example 2: javascript class constructor
class Cat {
constructor(name, age) {
this.name = name;
this.age = age;
}
get fullname() {
return this.getFullName()
}
getFullName() {
return this.name + ' ' + this.age
}
}
const run = document.getElementById("run");
run.addEventListener("click", function () {
let Skitty = new Cat('Skitty', 9);
let Pixel = new Cat('Pixel', 6);
console.log(Skitty.getFullName());
console.log(Skitty.fullname);
console.log(Skitty, Pixel);
})
Example 3: class declaration in javascript
class NameOfClass {
obj="text";
obj2="some other text";
}
console.log(new NameOfClass);