“Konstruktor kelas JavaScript” Kode Jawaban

Konstruktor kelas JavaScript

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}

const person = new Person("John Doe", 23);

console.log(person.name); // expected output: "John Doe"
TC5550

Kelas JavaScript

// Improved formatting of Spotted Tailed Quoll's answer
class Person {
	constructor(name, age) {
		this.name = name;
		this.age = age;
	}
	introduction() {
		return `My name is ${name} and I am ${age} years old!`;
	}
}

let john = new Person("John Smith", 18);
console.log(john.introduction());
Nathan uses Linux

JavaScript Konstruktor Kelas

class prettyMixedGirl {
    constructor(name, age, ethnicity, phoneNumber) {
        this.name = name;
        this.age = age;
        this.ethnicity = ethnicity;
        this.phoneNumber = phoneNumber;
    }
    // Method
    hi() {
        console.log(`Hi! My name is ${this.name}. I am ${this.age} years old. My ethnicity is ${this.ethnicity}. My phone number is ${this.phoneNumber}`);
    }
}
// Create new object out of Constructor (Instantiate)
const ashley = new prettyMixedGirl('Ashley', 28, 'Dominican Republican', '313-218-1345');
const luna = new prettyMixedGirl('Luna', 26, 'Chilean', '718-231-1343');

// Initiate a method
ashley.hi(); // Hi! My name is Ashley. I am 28 years old. My ethnicity is Dominican Republican. My phone number is 313-218-1345
luna.hi(); // Hi! My name is Luna. I am 26 years old. My ethnicity is Chilean. My phone number is 718-231-1343
Anthony Smith

Jawaban yang mirip dengan “Konstruktor kelas JavaScript”

Pertanyaan yang mirip dengan “Konstruktor kelas JavaScript”

Lebih banyak jawaban terkait untuk “Konstruktor kelas JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya