“JS Class” Ответ

JavaScript Class

class ClassMates{
	constructor(name,age){
    	this.name=name;
      	this.age=age;
    }
  	displayInfo(){
    	return this.name + "is " + this.age + " years old!";
    }
}

let classmate = new ClassMates("Mike Will",15);
classmate.displayInfo();  // result: Mike Will is 15 years old!
Spotted Tailed Quoll

javascript class phorgitance

// 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

JS Class

function Animal (name) {
  this.name = name;
}

Animal.prototype.speak = function () {
  console.log(`${this.name} makes a noise.`);
}

class Dog extends Animal {
  speak() {
    console.log(`${this.name} barks.`);
  }
}

let d = new Dog('Mitzie');
d.speak(); // Mitzie barks.

// For similar methods, the child's method takes precedence over parent's method
Dead Donkey

JS Class

class MyArray extends Array {
  // Overwrite species to the parent Array constructor
  static get [Symbol.species]() { return Array; }
}

let a = new MyArray(1,2,3);
let mapped = a.map(x => x * x);

console.log(mapped instanceof MyArray); // false
console.log(mapped instanceof Array);   // true
Dead Donkey

JS Class

const Animal = {
  speak() {
    console.log(`${this.name} makes a noise.`);
  }
};

class Dog {
  constructor(name) {
    this.name = name;
  }
}

// If you do not do this you will get a TypeError when you invoke speak
Object.setPrototypeOf(Dog.prototype, Animal);

let d = new Dog('Mitzie');
d.speak(); // Mitzie makes a noise.
Dead Donkey

JS Class

class MyClass {
  // class methods
  constructor() { ... }
  method1() { ... }
  method2() { ... }
  method3() { ... }
  ...
}
Puzzled Puffin

Ответы похожие на “JS Class”

Вопросы похожие на “JS Class”

Больше похожих ответов на “JS Class” по JavaScript

Смотреть популярные ответы по языку

Смотреть другие языки программирования