Still have a bit bug for highlight: <img width="1012" alt="image" src="https://github.com/user-attachments/assets/7ec39c85-c20c-4231-8752-b2a7a80f5570" />
83 lines
1.7 KiB
JavaScript
83 lines
1.7 KiB
JavaScript
const fs = require("fs");
|
|
|
|
const getName = function () {
|
|
return "John Doe";
|
|
};
|
|
|
|
/**
|
|
* A class representing a HelloWorld greeter with various utility methods
|
|
* @class HelloWorld
|
|
* @param {string} name - The name to use for greetings
|
|
*/
|
|
class HelloWorld {
|
|
// Version number of the HelloWorld class
|
|
static VERSION = "1.0.0";
|
|
// Counter to track number of class instances
|
|
static #instanceCount = 0;
|
|
|
|
// Private instance fields
|
|
#name;
|
|
#options;
|
|
#createdAt;
|
|
|
|
/**
|
|
* Creates a new HelloWorld instance
|
|
* @param {string} name - The name to use for greetings
|
|
* @param {Object} options - Configuration options
|
|
*/
|
|
constructor(name = "World", options = {}) {
|
|
this.#name = name;
|
|
this.#options = options;
|
|
this.#createdAt = new Date();
|
|
HelloWorld.#instanceCount++;
|
|
}
|
|
|
|
static getInstanceCount() {
|
|
return HelloWorld.#instanceCount;
|
|
}
|
|
|
|
get name() {
|
|
return this.#name;
|
|
}
|
|
|
|
set name(value) {
|
|
this.#name = value;
|
|
}
|
|
|
|
async greet(...names) {
|
|
try {
|
|
for (const name of names) {
|
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
console.log(`Hello, ${name}!`);
|
|
}
|
|
} catch (error) {
|
|
console.error(`Error: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
configure(options = {}) {
|
|
Object.assign(this.#options, options);
|
|
}
|
|
|
|
*generateSequence(start = 0, end = 10) {
|
|
for (let i = start; i <= end; i++) yield i;
|
|
}
|
|
|
|
processNames(names) {
|
|
return names
|
|
.filter((name) => name.length > 0)
|
|
.map((name) => name.toUpperCase())
|
|
.sort();
|
|
}
|
|
}
|
|
|
|
const greeter = new HelloWorld("JavaScript");
|
|
|
|
(async () => {
|
|
const uniqueNames = new Set(["Alice", "Bob"]);
|
|
await greeter.greet(...uniqueNames);
|
|
|
|
for (const num of greeter.generateSequence(0, 5)) {
|
|
console.log(num);
|
|
}
|
|
})();
|