Class Declaration - 01



NoteBook: ⇒ 

When we use the set method in a class, then we have to keep in mind that we have to set values from outside of the class basically with the class instance.



Class Inheritance:





class superClass { constructor(name, age, title) { this._name = name; this._age = age; this._title = title; } person() { return this._name; } get personTitle() { return this._age; } }; class subClass extends superClass { constructor(name, age, title) { /* Here with the super keyword we make available the constructor properties of parent class for child class. */ super(name, age, title); } get childGet () { return this._name; } } const supClass = new superClass('Mozahedul', 43); const personMethod = supClass.person(); console.log(personMethod); let getMethod = supClass.personTitle; console.log(getMethod); const childInstance = new subClass('rasel'); const resultGet = childInstance.childGet; console.log(resultGet); /* We can also call the properties and method of parent class inside the child class with the child class instance. */ const childResult = childInstance.person(); console.log(childResult);



class Man { constructor(name, age) { this.name = name; this.age = age; } play() { console.log(`my name is ${this.name}.`) } get setPerson() { console.log(`I am ${this.name} & my age is ${this.age}`) } set setPerson(name) { this.name = name; } } let person = new Man("mozahedu", 28); console.log(person); person.setPerson = "shahidul"; console.log(person.setPerson);



Post a Comment

Previous Post Next Post