Felix
0
Q:

setting property to method in child class javascript

// Class Inheritance in JavaScript
class Mammal {
	constructor(name) {
		this.name = name;
	}
	eats() {
		return `${this.name} eats Food`;
	}
}

class Dog extends Mammal {
	constructor(name, owner) {
		super(name);
		this.owner = owner;
	}
	eats() {
		return `${this.name} eats Chicken`;
	}
}

let myDog = new Dog("Spot", "John");
console.log(myDog.eats()); // Spot eats chicken
2
function Person(first, last, age, gender, interests) {
  this.name = {
    first,
    last
  };
  this.age = age;
  this.gender = gender;
  this.interests = interests;
};
1
// setting a new property in child class that extends parent or whatever

//PARENT CLASS
class Person{
  constructor(name,place){
  this.name = name; //property
  this.place = place; //property
  }
  Full(){ //method
    console.log(`I am ${this.name} from ${this.place}`);
  }
};

//CHILD CLASS EXTENDING PARENT
class Student extends Person{
  constructor(name,place,age){
    super(name,place);
    this.age = age; // always call super before accessing ("this")
  }
  Detail(){ //method
    this.area = "Adayar"; //NEW PROPERTY IN our CHILD METHOD
    console.log(`I'm ${this.name} from ${this.place}, area ${this.area} & I'm ${this.age} years old`);
  }
 Detailed(){ //method
    console.log(`${this.area}`); // it can still access in other method inside it's own child class
 }
};

var student1 = new Student("Surya", "Chenai", 25);
student1;
student1.Full();
student1.Detail();
student1.Detailed();
0

New to Communities?

Join the community