Answers for "js class"

73

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!
Posted by: Guest on November-20-2019
2

js class

class Car {
  constructor(brand, speed) {
    this.brand = brand
    this.speed = speed
  }
  speedUp() {
    this.speed += 5
    console.log(`The ${this.brand} is travelling at ${this.speed} mph`)
  }
  slowDown() {
    this.speed -= 5
    console.log(`The ${this.brand} is travelling at ${this.speed} mph`)

  }
}
const redCar = new Car('toyota', 0)
redCar.speedUp() // result: The toyota is travelling at 5 mph
redCar.slowDown() // result: The toyota is travelling at 0 mph
Posted by: Guest on April-27-2021
0

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
Posted by: Guest on October-08-2021
0

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
Posted by: Guest on October-08-2021
0

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.
Posted by: Guest on October-08-2021
0

js class

class ClassName {

   constructor() { ... }

  method_1() { ... }

  method_2() { ... }

  method_3() { ... }

}
Posted by: Guest on May-24-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language