“Membuat kelas JavaScript” Kode Jawaban

Kelas JavaScript

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

Kelas JavaScript

class Polygon {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }

  get area() {
    return this.calcArea();
  }

  calcArea() {
    return this.height * this.width;
  }
}

const square = new Polygon(10, 10);

console.log(square.area);
Perfect Pheasant

Membuat kelas JavaScript

// constructor function
function Person () {
    this.name = 'John',
    this.age = 23
}

// create an object
const person1 = new Person();
SAMER SAEID

Jawaban yang mirip dengan “Membuat kelas JavaScript”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya