“herencia de clase JavaScript” Código de respuesta

herencia de clase 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
Nathan uses Linux

Herencia de clase JavaScript

class Car {
  constructor(brand) {
    this.carname = brand;
  }
  present() {
    return 'I have a ' + this.carname;
  }
}

class Model extends Car {
  constructor(brand, mod) {
    super(brand);
    this.model = mod;
  }
  show() {
    return this.present() + ', it is a ' + this.model;
  }
}

let myCar = new Model("Ford", "Mustang");
document.getElementById("demo").innerHTML = myCar.show();
Clear Cottonmouth

herencia de clase JavaScript

// parent class
class Person { 
    constructor(name) {
        this.name = name;
    }

    greet() {
        console.log(`Hello ${this.name}`);
    }
}

// inheriting parent class
class Student extends Person {

}

let student1 = new Student('Jack');
student1.greet();
SAMER SAEID

herencia en clase en JS

//class in es6 are just functional constructor.
//Parent class is Person and Developer class inheritant from Person class using 
//extend and super method 
class Person{
  constructor(firstname,lastname){
    this.firstname= firstname;
    this.lastname=lastname;
    
  }
  aboutPerson(){
  console.log(`My name is ${this.firstname} ${this.lastname} `)
  }
}

class Developer extends Person{
constructor(firstname,lastname,experience,projects){
 /* //super keyword is used to call the constructor
 of its parent class to access the parent's properties and methods*/
	super(firstname,lastname);
  	this.experience=experience;
  	this.projects=projects;
  
  	aboutDev(){
      console.log(`My name is ${this.firstname} and  I have ${this.experience}
	in software development`)
}
  
  const ShirshakDev= new Developer('Shirshak','Kandel',3,13)
  console.log(ShirshakDev.aboutDev())
Shirshak kandel

Respuestas similares a “herencia de clase JavaScript”

Preguntas similares a “herencia de clase JavaScript”

Más respuestas relacionadas con “herencia de clase JavaScript” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código